C# WPF列表框绑定:SelectedItem未设置
我将一个列表框简化为以下XAMLC# WPF列表框绑定:SelectedItem未设置,c#,wpf,data-binding,mvvm,listbox,C#,Wpf,Data Binding,Mvvm,Listbox,我将一个列表框简化为以下XAML <ListBox ItemsSource="{Binding Properties}" DisplayMemberPath="Name" SelectedItem="SelectedProperty" /> 在我的ViewModel中: private List<Property> propertyList; private Property selectedProperty; public List<
<ListBox ItemsSource="{Binding Properties}"
DisplayMemberPath="Name"
SelectedItem="SelectedProperty" />
在我的ViewModel中:
private List<Property> propertyList;
private Property selectedProperty;
public List<Property> Properties
{
get
{
return propertyList;
}
set
{
propertyList = value;
NotifyPropertyChanged("Properties");
}
}
public Property SelectedProperty
{
get
{
return selectedProperty;
}
set
{
NotifyPropertyChanged("SelectedProperty");
selectedProperty= value;
}
}
私有列表属性列表;
私有财产选择财产;
公共列表属性
{
得到
{
返回属性列表;
}
设置
{
propertyList=值;
通知财产变更(“财产”);
}
}
公共财产选定财产
{
得到
{
返回selectedProperty;
}
设置
{
NotifyPropertyChanged(“SelectedProperty”);
selectedProperty=值;
}
}
我的列表框填充得很好,但无论我尝试什么,当我在列表框中选择一个项目时,我似乎都无法更新SelectedProperty。我尝试将其切换为使用observateCollection
而不是List
,并为CollectionChanged添加事件处理程序,但这没有起作用
我肯定我错过了一些愚蠢的事情,我看不见树木也看不见森林。我已经筋疲力尽了,需要有人帮忙 您需要绑定到
SelectedProperty
:
<ListBox ItemsSource="{Binding Properties}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedProperty}" />
知道这是显而易见的。谢谢