简体   繁体   English

WPF列表框绑定:SelectedItem未设置

[英]WPF Listbox binding: SelectedItem not setting

I have a ListBox simplified to the following XAML 我将ListBox简化为以下XAML

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

and in my ViewModel: 在我的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;
    }
}

My List box populates fine, but no matter what I try I cannot seem to get the SelectedProperty to update when I select an item in my list box. 我的列表框填充正常,但无论我尝试什么,当我在列表框中选择一个项目时,我似乎无法更新SelectedProperty。 I have tried switching it all around to use ObservableCollection rather than List and adding an Event handler for CollectionChanged, but this has not worked. 我已经尝试将其全部切换为使用ObservableCollection而不是List并为CollectionChanged添加事件处理程序,但这没有用。

I am sure I am missing something stupid, and cannot see the wood for the trees. 我确信我错过了一些愚蠢的东西,并且看不到树木的木头。 I am reaching the end of my tether and need someone to step in and help. 我到达了我的系绳的末端,需要有人介入并提供帮助。

You need to bind to the SelectedProperty : 您需要绑定到SelectedProperty

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

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

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