Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Issue in ListBox SelectedItem update

$
0
0

Hi

I am using Entity Framework in a project where ListBox is bound with ObservableCollection<tblYear> and SelectedItem is also bound with the property type of same Entity Object tblYear.

ViewModel:

private tblYear _SelectedYear;
        public tblYear SelectedYear
        {
            get { return _SelectedYear; }
            set
            { Set(ref _SelectedYear, value);}
        }

ObservableCollection<tblYear> _Years;
        public ObservableCollection<tblYear> Years
        {
            get { return _Years; }
            set {Set(ref _Years, value);}
        }

View:

<ListBox ItemsSource="{Binding Years, Mode=OneWay}" SelectedItem="{Binding SelectedYear, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"><ListBox.ItemTemplate><DataTemplate><StackPanel Orientation="Horizontal"><TextBlock Padding="5,0,5,0" Text="{Binding Year}" /></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox>

In other ViewModel code part, when I set SelectedYear property to tblYear entity object, it is not getting updated on screen.

Same issue is explained at - ComboBox’s SelectedItem not Displaying but here suggested that we should set SelectedValue field which will return only PrimaryKey value to ViewModel.  But I would like to have complete selected tblYear object in ViewModel.

In case of Entity Framework classes I am not able to understand the override implementation of ".Equals()".

Please suggest.



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>