WPF MVVM ComboBox SelectedItem or SelectedValue not working

前端 未结 18 2374
一生所求
一生所求 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:02

    I was fighting with this issue for a while. In my case I was using in complex type (List) as the Item Source and was using a KeyType as the selected value. On the load event, the KeyType was getting set to null. This caused everything to break. None of the sub elements would get updated when the key changed. It turned out that when I added a check to make sure the proposed value for KeyType was not null, everything worked as expected.

        #region Property: SelectedKey
        // s.Append(string.Format("SelectedKey : {0} " + Environment.NewLine, SelectedKey.ToString()));
    
        private KeyType _SelectedKey = new KeyType();
        public KeyType SelectedKey
        {
            get { return _SelectedKey; }
            set
            {
                if(value != null )
                    if (!_SelectedKey.Equals(value))
                    {
                        _SelectedKey = value;
                        OnPropertyChanged("SelectedKey");
                    }
            }
        }
        #endregion SelectedKey
    

提交回复
热议问题