我有一个简化为以下 XAML 的 ListBox
<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;
}
}
我的列表框填充正常,但无论我尝试什么,当我在列表框中选择一个项目时,似乎都无法更新 SelectedProperty。我已经尝试将其全部切换为使用 ObservableCollection
而不是 List
并为 CollectionChanged 添加事件处理程序,但这没有奏效。
我确信我错过了一些愚蠢的东西,只见树木不见森林。我快筋疲力尽了,需要有人介入并提供帮助。
最佳答案
您需要绑定(bind)到 SelectedProperty
:
<ListBox ItemsSource="{Binding Properties}"
DisplayMemberPath="Name"
SelectedItem="{Binding SelectedProperty}" />
关于c# - WPF 列表框绑定(bind) : SelectedItem not setting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10618883/