【问题标题】:Binding RadioButton to Dictionary将 RadioButton 绑定到字典
【发布时间】:2013-10-18 08:13:12
【问题描述】:

目前我有一个绑定到泛型类型的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(使用转换器返回真/假)。我最大的问题是字典以及如何将其绑定到 RadioButton 控件,以便我可以在屏幕上看到 2 个 RadioButton 选项。

有什么想法吗?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      这是一个使用 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>
      

      【讨论】:

        猜你喜欢
        • 2019-04-11
        • 2015-10-17
        • 2011-07-31
        • 1970-01-01
        • 2012-05-09
        • 2017-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多