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

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

I have a ComboBox like this



        
相关标签:
3条回答
  • 2021-01-15 06:52

    I used x:Bind for the ItemResource and added ViewModel inside code behind, and solved this problem.

    0 讨论(0)
  • 2021-01-15 06:52

    You ComboBox isn't blank, but it don't know how to render your MaxXXAgeMembers. You should use ItemTemplate to tell this to him. For Ex:

    <ComboBox
        Grid.Column="1"
        Padding="5,0,0,0"
        SelectedValue="{Binding MaxXXAge, Mode=TwoWay, Converter={StaticResource MaxXXAgeToMaxXXAgeMemberConverter}}"
        ItemsSource="{Binding ElementName=SettingsXXScrollViewer, Path=DataContext.MaxXXAgeMemberGroup, Mode=OneWay}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Description}" /> 
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    
    0 讨论(0)
  • 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<Person> 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.

    0 讨论(0)
提交回复
热议问题