替代元组(二传手)

时间:2016-02-02 15:51:15

标签: c# .net wpf xaml

我有以下ObservableCollection:

private ObservableCollection<Tuple<UInt32, String, String>> listValues = new ObservableCollection<Tuple<uint, string, string>>();

private Tuple<uint, string, string> selectedListValue;

我将集合绑定到ListBox:

<ListBox ItemsSource="{Binding ListValues, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                         DisplayMemberPath="Item1"
                                         SelectedItem="{Binding SelectedListValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

我的问题是,我得到一个例外,因为所选项目的元组没有设置器。有没有人知道这个问题的解决方法?

public Tuple<uint, string, string> SelectedListValue
{
    get { return selectedListValue; }
    set
    {
        selectedListValue= value;
        NotifyPropertyChanged("SelectedListValue");
    }
}

1 个答案:

答案 0 :(得分:4)

我建议在这种情况下使用这样的类:

public class MyClass
{
    public uint UintProp { get; set; }
    public string FirstString { get; set; }
    public string SecondString { get; set; }
}

private ObservableCollection<MyClass> listValues = new ObservableCollection<MyClass>();

它更灵活,并且设置了属性。