【问题标题】:WPF Listbox binding: SelectedItem not settingWPF 列表框绑定:SelectedItem 未设置
【发布时间】:2012-05-24 00:51:51
【问题描述】:

我有一个简化为以下 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 添加事件处理程序,但这没有奏效。

我确定我错过了一些愚蠢的东西,我只见树木不见森林。我已经走到了尽头,需要有人介入并提供帮助。

【问题讨论】:

    标签: c# wpf data-binding mvvm listbox


    【解决方案1】:

    你需要绑定到SelectedProperty:

    <ListBox ItemsSource="{Binding Properties}" 
     DisplayMemberPath="Name" 
     SelectedItem="{Binding SelectedProperty}"  />
    

    【讨论】:

    • 知道这很明显。谢谢。
    猜你喜欢
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2023-03-09
    • 2015-07-23
    • 1970-01-01
    • 2017-10-26
    • 2011-11-01
    相关资源
    最近更新 更多