C# 在C语言中将RadioButton绑定到int#
在XAML中创建RadioButton时,可以将状态绑定到int的值,如下所示 但是,如果在c#代码中将RadioButton添加到窗口中,我们如何添加这种绑定 按钮创建为C# 在C语言中将RadioButton绑定到int#,c#,xaml,data-binding,radio-button,C#,Xaml,Data Binding,Radio Button,在XAML中创建RadioButton时,可以将状态绑定到int的值,如下所示 但是,如果在c#代码中将RadioButton添加到窗口中,我们如何添加这种绑定 按钮创建为 RadioButton myRadBtn = new RadioButton(); myRadBtn.GroupName = "Some Group"; 我已经准备好了一个转换器类 public class RadBtnConverter : IValueConverter { //Convert a number
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;
}
}
有人能帮忙吗
我猜我需要并以某种方式将其附加到.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
,而不是声明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;
}
}
<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>
XAML代码:
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;
}
}
<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>
在第一个文本框中输入新的单选按钮的文本,然后单击该按钮。将创建一个新的RadioButton
对象,并将其正确绑定到RadioGroupValue
属性
还有第二个文本框
绑定到同一属性。它将显示当前的无线组值,您可以在那里编辑该值,无线组状态将更新以反映这一点(当焦点离开文本框时)。不应myBind。源
应分配给此
,即窗口
实例?为其指定myInt
属性的当前值似乎没有什么用处。此外,您当然应该确保myInt
属性以支持绑定的方式实现,例如作为dependencProperty
,使窗口implementIPropertyChanged
,等等。谢谢,这正好告诉了我需要知道的事情!我已经查过了,但仍然不知道如何正确使用DependencyProperty。如果您愿意的话,还有一个问题:我将绑定显示为附加到我的类中定义的本地int属性,以简化这个问题。但在我的项目中,我试图将它附加到一个类的属性上,这个类有一个实例。但是,我不能向该类添加DependencyProperty。有没有办法在这个窗口中创建一个链接到对象普通属性的窗口?@Skean:这取决于您想要的行为。但是对于双向绑定,您需要在绑定对象中实现一个受支持的绑定模型:dependencProperty
、INotifyPropertyChanged
,或者(如果我没记错的话)旧的XXXChanged
事件模型。我对WPF比较陌生,但我的理解是MVVM模式的一个用途是视图模型可以包装不支持绑定的对象。只要您的程序始终通过视图模型更改底层对象的属性值,一切都会正常工作。@Skean:FYI,刚刚尝试过,看起来绑定不支持较旧的XXXChanged
模型,至少不支持绑定
对象。谢谢,我发现绑定到属性就可以正常工作,只要我通过依赖项设置属性,它就会更新源并触发接口的其余部分进行更新。例如:使用myDP=dependencProperty.Register(“Var”、typeof(int)、typeof(main window))
在类中附加到myInstance.myVar
,它只是一个标准的intmyvar{set;get;}
。然后绑定有Source=myInstance
&Path=“Var”
我可以用SetValue(myDP,newVal)
设置。现在我需要找到一种方法来跟踪myDP,因为它是根据需要动态创建的:s