C# 将单选按钮绑定到字典
目前我有一个绑定到泛型类型的C# 将单选按钮绑定到字典,c#,wpf,C#,Wpf,目前我有一个绑定到泛型类型的单选按钮测试对象 在这个对象中,我有一个名为Value的属性,它可以是1或0。我有一个名为MyList的属性,它是一个字典,包含以下值: INT String 0 "This is the first option" 1 "This is the second option" 将单选按钮绑定到我的列表时遇到问题。目前我已检查绑定到值(使用转换器返回真/假)。我最大的问题是字典以及如何
单选按钮
<代码>测试对象
在这个对象中,我有一个名为Value的属性,它可以是1或0。我有一个名为MyList
的属性,它是一个字典
,包含以下值:
INT String
0 "This is the first option"
1 "This is the second option"
将单选按钮
绑定到我的列表
时遇到问题。目前我已检查绑定到值(使用转换器返回真/假)。我最大的问题是字典以及如何将它绑定到RadioButton
控件,以便在屏幕上看到2个RadioButton
选项
有什么想法吗?为什么不用键值对或元组列表代替字典呢
然后可以使用,将单选按钮放入数据模板中,并将ItemsSource绑定到KeyValuePairs列表。尝试在运行时填充单选按钮 下面是一个使用WPF的示例:
private RadioButton CreateRadioButton(RadioButton rb1, string content, Thickness margin, string name)
{
//private RadioButton CreateRadioButton(RadioButton rb1, string content, Thickness margin, string name)
// We create the objects and then get their properties. We can easily fill a list.
//MessageBox.Show(rb1.ToString());
Type type1 = rb1.GetType();
RadioButton instance = (RadioButton)Activator.CreateInstance(type1);
//MessageBox.Show(instance.ToString()); // Should be the radiobutton type.
PropertyInfo Content = instance.GetType().GetProperty("Content", BindingFlags.Public | BindingFlags.Instance);
PropertyInfo Margin = instance.GetType().GetProperty("Margin", BindingFlags.Public | BindingFlags.Instance);
PropertyInfo Name = instance.GetType().GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
// This is how we set properties in WPF via late-binding.
this.SetProperty<RadioButton, String>(Content, instance, content);
this.SetProperty<RadioButton, Thickness>(Margin, instance, margin);
this.SetProperty<RadioButton, String>(Name, instance, name);
return instance;
//PropertyInfo prop = type.GetProperties(rb1);
}
// Note: You are going to want to create a List<RadioButton>
private RadioButton CreateRadioButton(RadioButton rb1、字符串内容、厚度边距、字符串名称)
{
//专用RadioButton CreateRadioButton(RadioButton rb1、字符串内容、厚度边距、字符串名称)
//我们创建对象,然后获取它们的属性。我们可以很容易地填写一个列表。
//Show(rb1.ToString());
Type type1=rb1.GetType();
RadioButton实例=(RadioButton)Activator.CreateInstance(type1);
//MessageBox.Show(instance.ToString());//应为radiobutton类型。
PropertyInfo Content=instance.GetType().GetProperty(“Content”,BindingFlags.Public | BindingFlags.instance);
PropertyInfo Margin=instance.GetType().GetProperty(“Margin”,BindingFlags.Public | BindingFlags.instance);
PropertyInfo Name=instance.GetType().GetProperty(“Name”,BindingFlags.Public | BindingFlags.instance);
//这就是我们通过后期绑定在WPF中设置属性的方式。
this.SetProperty(内容、实例、内容);
此.SetProperty(边距、实例、边距);
this.SetProperty(名称、实例、名称);
返回实例;
//PropertyInfo prop=type.GetProperties(rb1);
}
//注意:您将要创建一个列表