我有一个 ComboBox,它在使用 MVVM 模式的 WPF 中让我感到悲伤

发布于 2024-08-31 03:22:12 字数 389 浏览 2 评论 0原文

这是我的代码:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与昨日 2024-09-07 03:22:12

您可以在控件关闭之前尝试“捕获”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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文