繁体   English   中英

WPF列表框绑定:SelectedItem未设置

[英]WPF Listbox binding: SelectedItem not setting

我将ListBox简化为以下XAML

<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添加事件处理程序,但这没有用。

我确信我错过了一些愚蠢的东西,并且看不到树木的木头。 我到达了我的系绳的末端,需要有人介入并提供帮助。

您需要绑定到SelectedProperty

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM