将单选按钮绑定到 C# 中的 int

本文关键字:中的 int 单选按钮 绑定 | 更新日期: 2023-09-27 18:36:16

在 XAML 中创建单选按钮时,可以将状态绑定到 int 的值,如本答案所示

但是,如果将单选按钮添加到 c# 代码中的窗口中,我们如何添加这种绑定?

按钮创建为

RadioButton myRadBtn = new RadioButton();
myRadBtn.GroupName = "Some Group";

我已经准备好了一个转换器类

public class RadBtnConverter : IValueConverter
{
    //Convert a number to radiobutton value by returning true if the radiobutton number (given as the parameter) is the same as the value passed in to convert
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var para = System.Convert.ToInt32(parameter);
        var myChoice = System.Convert.ToInt32(value);
        return para == myChoice;
    }
    //Convert the radiobutton to a number by checking isChecked, if true return the parameter as the convertion result, if false say to DoNothing
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var para = System.Convert.ToInt32(parameter);
        var isChecked = System.Convert.ToBoolean(value);
        return isChecked ? para : Binding.DoNothing;
    }
}

谁能帮忙?

我猜我需要进行绑定并以某种方式将其附加到.检查或.isCheck,我尝试:

//myInt is declared in the Window class with public int myInt {get;set;} and initialized to 0 in the class constructor
Binding myBind = new Binding("myInt");
myBind.Source = myInt;
myBind.Converter = new RadBtnConverter();
myBind.Mode = BindingMode.TwoWay;
myBind.ConverterParameter = 5;
myRadBtn.SetBinding(RadioButton.IsCheckedProperty, myBind);

虽然这没有给出任何错误,但 myInt 的值仍为 0。

将单选按钮绑定到 C# 中的 int

代码的问题在于您将 myInt 属性的当前值指定为 Binding.Source ,而不是声明 myInt 属性的对象的对象引用。

下面是一个代码示例,它执行您尝试执行的操作:

C# 代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    public int RadioGroupValue
    {
        get { return (int)GetValue(RadioGroupValueProperty); }
        set { SetValue(RadioGroupValueProperty, value); }
    }
    public static readonly DependencyProperty RadioGroupValueProperty = DependencyProperty.Register("RadioGroupValue", typeof(int), typeof(MainWindow));
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (textBox1.Text == "")
        {
            return;
        }
        RadioButton radioButton = new RadioButton();
        radioButton.Content = textBox1.Text;
        Binding binding = new Binding("RadioGroupValue");
        binding.Source = this;
        binding.Converter = new RadioButtonCheckedConverter();
        binding.ConverterParameter = stackPanel1.Children.Count;
        binding.Mode = BindingMode.TwoWay;
        radioButton.SetBinding(RadioButton.IsCheckedProperty, binding);
        stackPanel1.Children.Add(radioButton);
    }
}
class RadioButtonCheckedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int buttonId = (int)parameter, groupValue = (int)value;
        return buttonId == groupValue;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int buttonId = (int)parameter;
        bool isChecked = (bool)value;
        return isChecked ? buttonId : Binding.DoNothing;
    }
}

XAML 代码:

<Window x:Class="TestSO27791757DynamicRadioButton.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        x:Name="mainWindow1">
  <StackPanel>
    <Border BorderBrush="Black" BorderThickness="1" Margin="5" Padding="5">
      <StackPanel Orientation="Horizontal">
        <TextBox x:Name="textBox1" Width="100" Margin="0,0,10,0"/>
        <Button Content="Add RadioButton" Click="Button_Click"/>
        <TextBlock Text="Radio group value: " Margin="10,0,10,0" />
        <TextBox Text="{Binding ElementName=mainWindow1, Path=RadioGroupValue}" />
      </StackPanel>
    </Border>
    <Border BorderBrush="Black" BorderThickness="1" Margin="5" Padding="5">
      <StackPanel x:Name="stackPanel1" />
    </Border>
  </StackPanel>
</Window>

在第一个TextBox中输入新RadioButton的文本,然后单击按钮。将创建一个新的RadioButton对象,该对象正确绑定到 RadioGroupValue 属性。

还有第二个TextBox绑定到同一属性。它将显示当前的单选组值,您可以在此处编辑该值,并且单选按钮组状态将更新以反映该值(当焦点离开TextBox时)。