我已经仔细阅读了本网站上的所有相关问题,但出于某些原因,我无法用两个ListBox
填充我的两个ObservableCollection
es。是我的绑定问题还是模型中的某个地方?
型号:
public class DataModel : INotifyPropertyChanged
{
public ObservableCollection<object> List1
{
get
{
return this.List1;
}
set
{
this.List1 = value;
this.OnPropertyChanged("List1");
}
}
public ObservableCollection<object> List2
{
get
{
return this.List2;
}
set
{
this.List2 = value;
this.OnPropertyChanged("List2");
}
}
public DataModel(ObservableCollection<object> _list1, ObservableCollection<object> _list2)
{
this.List1 = _list1;
this.List2 = _list2;
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
XAML.cs:
public partial class UserControl1 : UserControl
{
public ObservableCollection<object> l;
public ObservableCollection<object> m;
public UserControl1()
{
l = new ObservableCollection<object> { "test1a", "test1b" };
m = new ObservableCollection<object> { "test2a", "test2b" };
InitializeComponent();
DataModel inst = new DataModel(l, m);
this.DataContext = inst;
this.TelProps.ItemsSource = l;
this.SurProps.ItemsSource = m;
}
}
XAML:
<Grid Name="Grid" DataContext="inst">
<ListBox Name="FirstProps"
DataContext="{Binding Source=inst}"
ItemsSource="{Binding List1}"
DisplayMemberPath="List1" />
<ListBox Name="SecondProps"
DataContext="{Binding Source=inst}"
ItemsSource="{Binding List2}"
DisplayMemberPath="List2" />
</Grid>
答案 0 :(得分:3)
您需要删除DisplayMemberPath
说明符。这是为了在List1
的每个项目上查找List1
属性。由于List1
仅包含一组字符串,因此List1
上没有System.String
属性,因此您会收到绑定错误。