我有以下 ObservableCollection:
private ObservableCollection<Tuple<UInt32, String, String>> listValues = new ObservableCollection<Tuple<uint, string, string>>();
private Tuple<uint, string, string> selectedListValue;
我正在将集合绑定(bind)到 ListBox:
<ListBox ItemsSource="{Binding ListValues, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Item1"
SelectedItem="{Binding SelectedListValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
我的问题是,我得到一个异常,因为所选项目的元组没有 setter 。有人知道这个问题的解决方法吗?
public Tuple<uint, string, string> SelectedListValue
{
get { return selectedListValue; }
set
{
selectedListValue= value;
NotifyPropertyChanged("SelectedListValue");
}
}
最佳答案
在这种情况下,我建议使用这样的类:
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>();
比较灵活,有set属性。
关于c# - 元组的替代方法(setter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35157933/