2

目前我有一个RadioButton绑定到泛型类型的...TestObject<T>

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

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

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

有任何想法吗?

4

2 回答 2

1

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

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

于 2012-10-24T06:38:16.210 回答
0

Try filling the radio buttons during runtime.

Here's an example using 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>
于 2013-10-18T00:13:12.970 回答