在 C# 中将 RadioButton 绑定到 int

Binding a RadioButton to an int in C#

在 XAML 中创建 RadioButton 时,您可以将状态绑定到 int 的值,如 shown in this answer

但是如果在c#代码中window添加了RadioButton,我们如何添加这种绑定?

按钮创建为

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

而且我有转换器class准备就绪

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;
    }
}

有人能帮忙吗?

我猜我需要 make a binding 并以某种方式将其附加到 .Checked 或 .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。

您的代码存在的问题是您将 myInt 属性 的当前值指定为 Binding.Source,而不是 [=12] 所在对象的对象引用=] 属性 已声明。

这是一个代码示例,可以执行您想要执行的操作:

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 时)。