项目源更改后保持单选选择

发布于 2024-08-28 20:15:57 字数 791 浏览 1 评论 0原文

设置:

我有一个组合框,它的 itemsource 绑定到 ObservableCollection对于自定义类,一个属性是 List

我有一个项目控件,它与组合框的选定项目列表进行数据绑定。财产。

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 work
IsChecked="{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 技术交流群。

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

发布评论

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

评论(1

云雾 2024-09-04 20:15:57

我也不知道如何在 xaml 中使用窗口作为值转换器。相反,创建一个具有枚举类型公共属性的独立值转换器类。接下来,在窗口的构造函数中,获取对值转换器实例的引用并将其存储在私有成员中。

XAML:

<local:MyValueConverter x:Key="ConvertSomething" />

隐藏代码:

private MyValueConverter _myValueConverter;

public Window1()
{
  InitializeComponent();

  _myValueConverter = FindResource("ConvertSomething") as MyValueConverter;
}

private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
    // You can access _myValueConverter here and set its public enum property.    
}

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:

<local:MyValueConverter x:Key="ConvertSomething" />

Code behind:

private MyValueConverter _myValueConverter;

public Window1()
{
  InitializeComponent();

  _myValueConverter = FindResource("ConvertSomething") as MyValueConverter;
}

private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
    // You can access _myValueConverter here and set its public enum property.    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文