C# - 具有动态数量可绑定(bind)属性的类

标签 c# silverlight data-binding binding

我有 X 个单选按钮 (Silverlight)。通常这个值在2-5之间。 称之为“开关”。每个单选按钮都绑定(bind)到该类的一个属性。

示例属性如下

private bool _radio1;
public bool Radio1{
 set{
  _radio1 = value;
  _radioX = !value; // make all others false
 }
 get{ return _radio1 }
}

基本思想是,如果选择了一个 radio ,则所有其他 radio 都将关闭。

我尝试过使用其他方法,与使用模板创建列表相比,这似乎是最简单的(特别是当我在某些情况下有单选涉及的其他按钮时)

目前我有两个要求,我需要一个具有 2 个属性(例如,男性/女性)复选框和 3 个属性的类。

将来可能会有更多,因此我认为为 X 数量的单选按钮编写一个新类是愚蠢的。

有没有办法以某种方式使属性的数量动态化?我看到了一些字典方法。

我看到了这种方法 How do I create dynamic properties in C#?

但我不知道如何绑定(bind)它。

最佳答案

虽然使用 C# 的动态功能很有趣,但在本例中不需要。良好的老式数据绑定(bind)功能足够强大,可以完成我们想要的事情。例如。下面是一些使用 RadioButton 模板将 ItemsControl 绑定(bind)到集合的 XAML:

<Grid>
    <StackPanel>
        <ItemsControl ItemsSource="{Binding}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <RadioButton
                        GroupName="Value"
                        Content="{Binding Description}"
                        IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <TextBlock Text="{Binding SelectedItem}"/>
    </StackPanel>
</Grid>

以下是如何在代码隐藏或 View 模型中使用它:

DataContext = new CheckBoxValueCollection(new[] { "Foo", "Bar", "Baz" });

现在您所需要的只是使其工作的集合。以下是复选框值的集合:

public class CheckBoxValueCollection : ObservableCollection<CheckBoxValue>
{
    public CheckBoxValueCollection(IEnumerable<string> values)
    {
        foreach (var value in values)
        {
            var checkBoxValue = new CheckBoxValue { Description = value };
            checkBoxValue.PropertyChanged += (s, e) => OnPropertyChanged(new PropertyChangedEventArgs("SelectedItem"));
            this.Add(checkBoxValue);
        }
        this[0].IsChecked = true;
    }

    public string SelectedItem
    {
        get { return this.First(item => item.IsChecked).Description; }
    }
}

以下是复选框值本身:

public class CheckBoxValue : INotifyPropertyChanged
{
    private string description;
    private bool isChecked;

    public string Description
    {
        get { return description; }
        set { description = value; OnPropertyChanged("Description"); }
    }
    public bool IsChecked
    {
        get { return isChecked; }
        set { isChecked = value; OnPropertyChanged("IsChecked"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

现在您拥有完全动态的数据驱动和数据绑定(bind)友好的单选按钮设计。

示例程序如下所示:

enter image description here

单选按钮下方的文本显示当前选定的项目。尽管在此示例中我使用了字符串,但您可以轻松更改设计以使用枚举值或单选按钮描述的其他来源。

关于C# - 具有动态数量可绑定(bind)属性的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6063974/

相关文章:

c# - 二维数组的行和列大小

c# - 为什么是私有(private)二传手?

silverlight - 如何检测断开的双工轮询客户端

c# - Silverlight 不适用于网络的不同 IP 段

wpf - 如何在数据模板中实例化 ViewModel

javascript - SAPUI5数据绑定(bind)获取更改事件中更改的值

javascript - polymer 绑定(bind)数据到使用 innerHTML 加载的元素

c# - Excel 文件生成器 : How to test resulting file

c# - DataGridView 替换值

Silverlight InlineCollection.Add(InlineUIContainer) 丢失?