2012-10-23 57 views
2

目前我有一個綁定到一個通用類型RadioButton ... TestObject<T>綁定單選按鈕,以字典

在該目的,我有一個名爲Value屬性可以是1或0。我有一個稱爲屬性MyList這是一個Dictionary<T,String>持有以下值:

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

我無法做綁定RadioButtonMyList什麼。目前,我已將IsCheck綁定到Value(使用轉換器返回true/false)。我最大的問題是字典和如何綁定到RadioButton控制,所以我可以在屏幕上看到2 RadioButton選項。

任何想法?

回答

1

爲什麼不使用KeyValuePair或Tuple列表而不是字典?

然後你可以使用ItemsControl,把RadioButton放到你的DataTemplate中,並將ItemsSource綁定到你的KeyValuePairs列表。

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>