0

Fellow coders!

I have an app where ComboBox is bound to the list of EF Core entities. What I found is when I bind it to SelectedItem it will not return the correct item in a disconnected scenario. However, when using both SelectedValue and SelectedItem, it seems to work fine. Is this a good approach? Parent object holds both "int Id" and Navigation Property. So SelectedItem is bound to Navigation Property and SelectedValue to "int Id".

C#:

            public Person()
            {
              int Id {get; set;}
              string Name {get; set;}
            }

XAML:

     <ComboBox
           DisplayMemberPath="Name"
           SelectedValuePath="Id"
           ItemsSource="{Binding People}"
           SelectedValue="{Binding SelectedPerson, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
           SelectedItem="{Binding SelectedPersonNavigation", UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
Konrad Psiuk
  • 181
  • 1
  • 11
  • You haven't made it quite clear what you mean by _it will not return the correct item_. But take a look at this for the difference between `SelectedItem` and `SelectedValue`. You don't need to use both of those in conjecture, it's generally either, and if you use `SelectedValue` generally you will need to use `SelectedValuePath` as well. https://stackoverflow.com/questions/4902039/difference-between-selecteditem-selectedvalue-and-selectedvaluepath – Sach Jul 31 '20 at 19:13
  • 1
    As a note, for both properties it is pointless to set `UpdateSourceTrigger=PropertyChanged` on the Binding, because that is already the default. For a OneWay Binding it doesn't make sense anyway, because UpdateSourceTrigger only has an effect in TwoWay or OneWayToSource Bindings. – Clemens Jul 31 '20 at 19:20
  • Sorry, I'll make it more precise: I'm loading Parent entity from DB, also a list of People. What I try to achieve is when I load those objects, I want Combobox to show selected Person from the Parent entity. Another think is when calling SaveChanges and changing only the SelectedValue I end with different Id and Navigation Property. I guess I can achieve the same by calling Navigation Property = null before the SaveChanges call and just using SelectedValue – Konrad Psiuk Jul 31 '20 at 19:25
  • Please edit the original post and show the mentioned code. – Sach Jul 31 '20 at 19:32

1 Answers1

0

Got the solution

  public class Person
    {
        public int Id {get; set;}
        string Name {get; set;}
    }

    public class ViewModel:INotifyPropertyChanged
    {
        private int _personId;
        public ObservableCollection<Person> People { get; set; }

        public int PersonId
        {
            get => _personId;
            set
            {
                _personId = value; 
                OnPropertyChanged(nameof(PersonId));
                OnPropertyChanged(nameof(SelectedPerson));
            }
        }

        public Person SelectedPerson
        {
            get => People.First(x => x.Id == PersonId);
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
Konrad Psiuk
  • 181
  • 1
  • 11