Why this UWP ComboBox can be blank after initialization but works fine for selection?

后端 未结 3 1069
花落未央
花落未央 2021-01-15 06:17

I have a ComboBox like this



        
3条回答
  •  生来不讨喜
    2021-01-15 07:02

    It's blank because you don't have anything selected in the first place. If I'm not mistaken, you have to either use SelectedItem to bind your selection or SelectedValue with SelectedValuePath.

    I actually never use SelectedValue with SelectedValuePath myself, so after initializing collection of items which ComboBox.ItemSource will be binded to - for example ObservableCollection Persons {get; set;} - I also set selected item property Person SelectedPerson {get; set;} to one of the values from collection. Then I bind ComboBox.SelectedItem to this property, so on initialization it shows predefined selected value.

    I guess you can achieve the same with SelectedValue and SelectedValuePath, but you have to use them together as described here.

提交回复
热议问题