项目源更改后保持单选选择
设置:
我有一个组合框,它的 itemsource 绑定到 ObservableCollection
我有一个项目控件,它与组合框的选定项目列表
itemscontrol 数据模板创建一个单选按钮列表,每个单选按钮代表列表中的各个枚举值。
愿望:
当我更改组合框中的值时,项目控件源会更新。我想要发生的是,如果新项目控件源中的单选按钮与上一个列表中选定的单选按钮(更新之前)相同,则要检查这一点。
当前想法:
将 Checked 事件分配给单选按钮,该事件在窗口类中维护一个可以进行比较的 myenum 属性。将单选框的 IsChecked 属性绑定到转换器并与 myenum 属性进行比较。为了实现这一点,我使窗口类从 IValueConverter 扩展,这样转换器函数就可以访问 myenum 属性。
问题:
我不知道如何获取 IsChecked 绑定以使用窗口作为转换器。我尝试在绑定的转换器部分使用相对源,但这不起作用 IsChecked="{Binding Converter={RelativeSource={RelativeSource Self}}}"
首选答案:
如果可能的话,帮助纠正绑定语法。
实现我想要的目标的更合适方法的想法。
Setup:
I have a combo-box, it's itemsource bound to an ObservableCollection<T> of a custom class, one property is a List<myenum>.
I have an itemscontrol which is databound to the combo-box's selected item List<myenum> property.
The itemscontrol datatemplate creates a list of radiobuttons, each representing the individual enum values in the list.
The Desire:
When I change the value in the combo-box the itemscontrol source is updated. What I want to occur, is if a radio button in the new itemscontrol source is the same as the selected radiobuton in the previous list (before it was updated), this to be checked.
Current Idea:
Assign a Checked event to the radio buttons, which maintains a myenum property in the window class which can be compared against. Make the IsChecked property of the radiobox bind to a converter and compare against the myenum property. To achieve this, I have made the window class extend from IValueConverter, this way the converter function has access to the myenum property.
Issue:
I don't know how to get the IsChecked binding to use the window as the converter. I have tried using relative source in the converter part of the binding, but that doesn't workIsChecked="{Binding Converter={RelativeSource={RelativeSource Self}}}"
Preferred Answers:
Assistance on correcting the binding syntax if it's possible this way.
Ideas of a more appropriate way of achieving what I'd like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也不知道如何在 xaml 中使用窗口作为值转换器。相反,创建一个具有枚举类型公共属性的独立值转换器类。接下来,在窗口的构造函数中,获取对值转换器实例的引用并将其存储在私有成员中。
XAML:
隐藏代码:
I also do not know how to use the window as a value converter in xaml. Instead create a standalone value converter class with a public property for the enum type. Next, in the constructor of the window, get a reference to the instance of the value converter and store it in a private member.
XAML:
Code behind: