C# 一组RadioButton控件通过绑定设置WPF DependencyProperty

C# 一组RadioButton控件通过绑定设置WPF DependencyProperty,c#,wpf,xaml,binding,dependency-properties,C#,Wpf,Xaml,Binding,Dependency Properties,我试图创建一个简单的UserControl,它包含一组单选按钮,然后将单个DependencyProperty设置为一个char值(每个单选按钮都有一个与之关联的唯一char值)。我从这篇文章中得到启示,这似乎是一个优雅的解决方案,但我无法让它发挥作用。选中其中一个单选按钮不会更改属性。ValueConverter的两个方法都未被调用。运行时没有编译时错误或绑定错误。我错过了什么 这是我的XAML: <UserControl x:Class="TestClientWpf.OrderType

我试图创建一个简单的UserControl,它包含一组单选按钮,然后将单个DependencyProperty设置为一个char值(每个单选按钮都有一个与之关联的唯一char值)。我从这篇文章中得到启示,这似乎是一个优雅的解决方案,但我无法让它发挥作用。选中其中一个单选按钮不会更改属性。ValueConverter的两个方法都未被调用。运行时没有编译时错误或绑定错误。我错过了什么

这是我的XAML:

<UserControl x:Class="TestClientWpf.OrderTypePicker"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:TestClientWpf="clr-namespace:TestClientWpf">
<WrapPanel>
    <WrapPanel.Resources>
        <TestClientWpf:CharMatchToBooleanConverter x:Key="converter" />
    </WrapPanel.Resources>
    <RadioButton IsChecked="{Binding Path=OrderType, Mode=TwoWay, Converter={StaticResource converter}, ConverterParameter=1}">Type 1</RadioButton>
    <RadioButton IsChecked="{Binding Path=OrderType, Mode=TwoWay, Converter={StaticResource converter}, ConverterParameter=2}">Type 2</RadioButton>
    <RadioButton IsChecked="{Binding Path=OrderType, Mode=TwoWay, Converter={StaticResource converter}, ConverterParameter=3}">Type 3</RadioButton>
</WrapPanel>
</UserControl>
我的ValueConverter:

public class CharMatchToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || parameter == null)
            return false;

        string checkValue = value.ToString();
        string targetValue = parameter.ToString();
        return checkValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || parameter == null)
            return null;

        bool useValue = (bool)value;
        string targetValue = parameter.ToString();
        return useValue ? char.Parse(targetValue) : (char?) null;
    }

在UserControl中,DataContext仍然指向其父级的正常DataContext

您需要绑定到控件本身的属性,因此:

<RadioButton IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=OrderType, Mode=TwoWay, Converter={StaticResource converter}, ConverterParameter=1}">Type 1</RadioButton>
类型1

在UserControl中,您的DataContext仍然指向其父控件的正常DataContext

您需要绑定到控件本身的属性,因此:

<RadioButton IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=OrderType, Mode=TwoWay, Converter={StaticResource converter}, ConverterParameter=1}">Type 1</RadioButton>
类型1

对于记录,我的一位同事还提供了另一种解决方案:将DataContext属性设置为UserControl实例(例如在构造函数中):


作为记录,我的一位同事还提供了另一种解决方案:将DataContext属性设置为UserControl实例(例如在构造函数中):


如果需要解释,请参阅此链接

或者你可以使用

  `Radio1.GroupName = Radio2.GroupName = this.GetHashCode().ToString() + Radio1.GroupName;`

如果需要解释,请参阅此链接

或者你可以使用

  `Radio1.GroupName = Radio2.GroupName = this.GetHashCode().ToString() + Radio1.GroupName;`

刚刚尝试了这个,仍然不起作用:同样的事情发生了,代码编译了,但是属性没有随着检查RadioButton而改变。抱歉,绑定到RadioButton,已经更改了我的答案以找到控件刚刚尝试了这个,仍然不起作用:同样的事情发生了,代码已编译,但属性不会因检查RadioButton而更改。很抱歉,已绑定到RadioButton,已更改我的答案以查找控件。不幸的是,如果控件中的某些内容要绑定到真实的DataContext,这将破坏您的控件。不幸的是,如果控件中的某些内容要绑定到真实的DataContext,这将破坏您的控件。