将RadioButton绑定到Dictionary

时间:2012-10-23 21:02:32

标签: c# wpf

目前我有RadioButton绑定到通用类型... TestObject<T>

在这个对象中,我有一个名为Value的属性,可以是1或0.我有一个名为MyList的属性,它是一个Dictionary<T,String>,它包含以下值:

   INT          String
    0            "This is the first option"
    1            "This is the second option"

我在将RadioButton绑定到MyList时遇到了什么问题。目前我将IsCheck ed绑定到Value(使用转换器返回true / false)。我最大的问题是字典以及如何将其绑定到RadioButton控件,以便在屏幕上看到2个RadioButton选项。

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

为什么不使用KeyValuePairs或Tuples列表而不是字典?

然后您可以使用ItemsControl,将RadioButton放入DataTemplate并将ItemsSource绑定到KeyValuePairs列表。

答案 1 :(得分:0)

尝试在运行时填充单选按钮。

以下是使用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>
相关问题