我有一个 ComboBox,它在使用 MVVM 模式的 WPF 中让我感到悲伤
这是我的代码:
<ComboBox Grid.Column="1" Grid.Row="9" ItemsSource="{Binding Path=PriorityEntries}" SelectedItem="{Binding Path=Priority,Mode=TwoWay}"/>
组合框与 PriorityEntries 正确绑定,当我更改组合框的值时,调用绑定属性(优先级)的“设置”,将其设置为所需的值。但是,当我关闭此组合框所在的 UserControl 时,它会再次使用 null 值调用 set 属性,然后将 selectedItem 设置为 null。为什么当我关闭用户控件时组合框再次被绑定。我尝试将模式设置为 OneTime,但这不会反映任何更改......
Here's my code:
<ComboBox Grid.Column="1" Grid.Row="9" ItemsSource="{Binding Path=PriorityEntries}" SelectedItem="{Binding Path=Priority,Mode=TwoWay}"/>
The comboBox is bound properly with PriorityEntries, and when i change the value of the comboBox the "set" of the bound property(Priority) is called setting it to what it needs to be. However, when i close the UserControl that this combobox resides, it calls the set property again with a value of null and then sets what the selectedItem was to null. Why is the comboBox being bound again when I close the usercontrol. I tried setting the mode to OneTime, but that won't reflect any changes...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在控件关闭之前尝试“捕获”ViewModel 中属性中的优先级值并使用捕获的值。您还可以修改优先级的设置器,以便在关闭与 ViewModel 关联的控件后不允许对其进行设置。
You can try 'capturing' the Priority value in a property in your ViewModel before the control closes and use the captured value. You could also modify the setter for Priority so that it doesn't allow it to be set once the control associated with the ViewModel has been closed.