XAML ComboBox SelectionChanged 触发 OnLoad
如果我有一个具有 SelectionChanged 事件的 ComboBox,它会在我加载控件时触发。
因此,在页面加载时,我设置了 SelectedValue 并触发 SelectionChanged 事件,这不是我想要发生的情况。
阻止这种情况的公认方法是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对此的两个明显的解决方案是 1) 等待包含 ComboBox 的 Window/Page/UserControl 的 Loaded 事件并在那里挂钩 SelectionChanged...例如在构造函数中:
或 2) 检查 ComboBox 是否已加载到选择中在执行任何操作之前更改处理程序,如果没有,则返回...例如在处理程序中:
我更喜欢数字 1,因为它不需要每次触发 SelectionChanged 处理程序时进行检查。
Two obvious solutions to this would be 1) Wait until the Loaded event of the Window/Page/UserControl which contains the ComboBox and hook up SelectionChanged there...eg in the constructor:
or 2) Check that the ComboBox has loaded in the selection changed handler before doing anything and return if it hasn't...eg in the handler:
I would prefer number 1 as it doesn't require the check every time the SelectionChanged handler is fired.
我遇到了一种特殊情况:
如果您正在使用枢轴,并且过早触发控制位于 PivotItem > > 中。 0,你仍然会遇到这个问题。
在这种情况下,似乎 this.Loaded() 仅引用 PivotItem“0”,并且更改为 UI 中的其他 PivotItem 无论如何都会触发事件。
在这种情况下,解决方案是(以您的示例为例):
I faced a particular situation :
If you are using a pivot, and prematurely firing control is in PivotItem > 0, you will still have the problem.
In that case, it seems this.Loaded() only refers to PivotItem "0", and changing to other PivotItem in the UI will fire events anyway.
In that case, the solution is (with your example) :