c# - WPF 列表框绑定(bind) : SelectedItem not setting

标签 c# wpf data-binding mvvm listbox

我有一个简化为以下 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/

相关文章:

c# - Button中的ComboBox,如何将Combobox的 "SelectedItem"作为Button的CommandParameter传递?

javascript - 纯 Javascript 双向数据绑定(bind)

c# - 使用c#删除事件目录中的用户

c# - C# 旋转函数的奇怪行为

c# - 加载新的 WPF/C# 窗口时触发的最后一个事件是什么?

c# - 将一个类中的值绑定(bind)到另一类中的另一个值

c# - .net 2.0 的 Dispatcher.Invoke 模拟

javascript - Knockout、其他对象内的 ViewModel 和数据绑定(bind)

C# - 使用关联程序打开文件而不调用命令行

c# - 'globally'如何捕获对象实例中抛出的异常