将 (TwoWay) 字符串绑定到两个包含内容的单选按钮

bind (TwoWay) string to two radio buttons with content

我想将字符串值("True" 或 "False")绑定 (TwoWay) 到内容为 "True" 和 "False" 的两个单选按钮。

条件

  1. 当使用 select "True" RadioButton 时,字符串值应为 "True",反之亦然 "False" 单选按钮。
  2. 一次只有一个 RadioButton select可以是 True 或 False。
  3. 有字符串列表,单选按钮是 DataTemplate 的一部分。

XAML

<Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<ListBox ItemsSource="{Binding Values}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <RadioButton x:Name="rbTrue" Content="True" IsChecked="{Binding Value,Mode=TwoWay}" />
                <RadioButton x:Name="rbFalse" Content="False" IsChecked="{Binding Value,Mode=TwoWay}"/>
            </StackPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=Value,Mode=TwoWay}" Value="True">
                <Setter Property="IsChecked" TargetName="rbTrue" Value="True"/>
                <Setter Property="IsChecked" TargetName="rbFalse" Value="False"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=Value,Mode=TwoWay}" Value="False">
                <Setter Property="IsChecked" TargetName="rbTrue" Value="False"/>
                <Setter Property="IsChecked" TargetName="rbFalse" Value="True"/>
            </DataTrigger>
        </DataTemplate.Triggers>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

XAML.CS

public partial class MainWindow : Window
{
    MainViewModel viewModel;

    public MainWindow()
    {
        InitializeComponent();

        viewModel = new MainViewModel();

        this.DataContext = viewModel;
    }
}

VIEWMODEL

public class MainViewModel : ModelBase
{
    private List<MainModel> values;
    public List<MainModel> Values
    {
        get
        {
            return values;
        }
        set
        {
            if (value != values)
            {
                values = value;
                OnPropertyChanged("Values");
            }
        }
    }

    public MainViewModel()
    {
        Values = new List<MainModel>();

        for (int i = 0; i < 10; i++)
        {
            if (i % 2 == 0)
                Values.Add(new MainModel() { Value = true });
            else
                Values.Add(new MainModel() { Value = false });
        }
    }
}

型号

public class MainModel : ModelBase
{
    private bool strValue;
    public bool Value
    {
        get
        {
            return strValue;
        }
        set
        {
            if (value != strValue)
            {
                strValue = value;
                OnPropertyChanged("Value");
            }
        }
    }
}

模型库

public abstract class ModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

UI

我几乎尝试了我所知道的一切,还在不同论坛和其他 material.

在线检查了大部分相关链接

我只想要 "True" 或 "False" 中基于列表中用户 selection 的值。

提前致谢。

我假设你的意思是 "bind (TwoWay) Boolean value to two radio buttons" 而不是 "bind (TwoWay) string to two radio buttons",因为 Value 的数据类型是 bool

首先,不要使用DataTrigger;删除它们。

相反,在 "false" 按钮的绑定上,添加一个取反布尔值的转换器:

<RadioButton x:Name="rbFalse" Content="False" IsChecked="{Binding Value,Mode=TwoWay,Converter={StaticResource NegateConverter}}"/>

在您的 XAML 资源中,您需要定义转换器:

<local:NegateConverter x:Key="NegateConverter"/>

local 替换为您在 NegateConverter class 中定义的任何命名空间。

这是转换器的代码:

/// <summary>
/// This converter negates a boolean value.
/// </summary>
[ValueConversion(typeof(bool), typeof(bool))]
public class NegateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool bValue = false;
        if (value is bool)
        {
            bValue = (bool)value;
        }
        return !bValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool bValue = false;
        if (value is bool)
        {
            bValue = (bool)value;
        }
        return !bValue;
    }
}