WPF MVVM ComboBox SelectedItem or SelectedValue not working

前端 未结 18 2441
一生所求
一生所求 2020-12-05 06:22

Update

After a bit of investigating. What seems to be the issue is that the SelectedValue/SelectedItem is occurring before the Item source is finis

18条回答
  •  囚心锁ツ
    2020-12-05 07:16

    I have a very simply answer for this problem. First add the following code to the View IsSynchronizedWithCurrentItem="True".

    Next when ever you assign a new object in the ViewModel to that Property SelectedObject should be saved to that Property and not the private member.

    The viewmodel Proptery should look like this

        public Role SelectedObject 
        {
            get { return object; }
            set
            {
                if (value != null)
                {
                    if (!object.Equals(value))
                    {
                        object = value;
                        OnPropertyChanged(() => SelectedObject );
                    }
                }
            }
        }
    

    This should fix the issue.

提交回复
热议问题