WPF ComboBox with CompositeCollection - SelectedIndex not sticking

前端 未结 1 1525
我寻月下人不归
我寻月下人不归 2021-01-06 10:52

I\'m using a ComboBox with a CompositeCollection as follows:


    
        
                   


        
相关标签:
1条回答
  • 2021-01-06 11:07

    Since you're adding to your Collection after the ComboBox is constructed, you may have to dip into the Loaded event and set your SelectedIndex there...

    <ComboBox Loaded="ComboBox_Loaded">
        <ComboBox.ItemsSource>
            <CompositeCollection>
                <ComboBoxItem Content="All" />
                <CollectionContainer Collection="{Binding Source={StaticResource AllBitsSource}}" />
            </CompositeCollection>
        </ComboBox.ItemsSource>
    </ComboBox>
    

    Code behind:

    private void ComboBox_Loaded(object sender, RoutedEventArgs e)
    {
        (sender as ComboBox).SelectedIndex = 0;
    }
    
    0 讨论(0)
提交回复
热议问题