sushi7777
sushi7777

Reputation: 189

How to select "None" as the first selected item once the combobox is loaded on the UI - WPF

I can't seem to get my combobox to display the "None" item i inserted into my composite collection once the combobox is loaded. If a user decides to change it afterwards, I don't care, but the initial load should set the "None" to be selected first. Here is my XAML

<DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
                <ComboBox DisplayMemberPath="SName" SelectedItem="{Binding BModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"                                SelectedValuePath="GId"             SelectedValue="{Binding BModel.GId, Mode=TwoWay}"
                                                                              HorizontalContentAlignment="Stretch"
                                                                              MinWidth="{Binding ElementName=BAssigned, Path=MinWidth}" 
                                                                              Style="{StaticResource SPanelComboBox}">
                       <ComboBox.ItemsSource>
                              <CompositeCollection>
                                    <emodels:SModel SName="None" GId="-1"/>
                                    <CollectionContainer Collection="{Binding DataContext.BListModels, 
                                                                            Source={x:Reference SDataGrid}}"/>
                              </CompositeCollection>
                       </ComboBox.ItemsSource>
                   </ComboBox>
         </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
                                                            
<DataGridTemplateColumn.CellEditingTemplate >
            <DataTemplate>
                          <ComboBox SelectedItem="{Binding BModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                                                              HorizontalContentAlignment="Stretch"
                                                                              Style="{StaticResource SPanelComboBox}">
                               <ComboBox.ItemsSource>
                                    <CompositeCollection>
                                               <emodels:SModel SName="None" GId="-1"/>
                                             <CollectionContainer Collection="{Binding DataContext.BListModels, 
                                                                            Source={x:Reference SDataGrid}}"/>
                                    </CompositeCollection>
                               </ComboBox.ItemsSource>
                      </ComboBox>
               </DataTemplate>
         </DataGridTemplateColumn.CellEditingTemplate>
  </DataGridTemplateColumn>

Upvotes: 0

Views: 585

Answers (1)

EldHasp
EldHasp

Reputation: 7943

Take a look at examples of setting SelectedIndex and SelectedItem in a ComboBox.

<Window.Resources>
    <sys:String x:Key="zero">Zero</sys:String>
    <spc:StringCollection x:Key="score">
        <sys:String>First</sys:String>
        <sys:String>Second</sys:String>
        <sys:String>Third</sys:String>
    </spc:StringCollection>
    <CompositeCollection x:Key="strings">
        <StaticResource ResourceKey="zero"/>
        <CollectionContainer Collection="{Binding Mode=OneWay, Source={StaticResource score}}"/>
    </CompositeCollection>
</Window.Resources>
<StackPanel Orientation="Horizontal">

    <!--Only index specified-->
    <ComboBox Width="100" VerticalAlignment="Top" 
              ItemsSource="{Binding Mode=OneWay, Source={StaticResource strings}}"
              SelectedIndex="0" />

    <!--Only SelectedItem set-->
    <ComboBox Width="100" VerticalAlignment="Top" 
              ItemsSource="{Binding Mode=OneWay, Source={StaticResource strings}}"
              SelectedItem="First" />

    <!--SelectedIndex and SelectedItem set to the same item-->
    <ComboBox Width="100" VerticalAlignment="Top" 
              ItemsSource="{Binding Mode=OneWay, Source={StaticResource strings}}"
              SelectedIndex="0"
              SelectedItem="Zero" />

    <!--SelectedIndex and SelectedItem set to different items-->
    <ComboBox Width="100" VerticalAlignment="Top" 
              ItemsSource="{Binding Mode=OneWay, Source={StaticResource strings}}"
              SelectedIndex="0"
              SelectedItem="First" />

</StackPanel>

The example shows that SelectedItem has a higher priority than SelectedIndex. And if both properties are set then SelectedItem will be used.

In your code, the BModel property contains an item from the BListModels collection or is null.
And using SelectedIndex = 0 you want to set the SelectedItem to SName = "None".
Since SelectedItem has a different meaning, setting SelectedIndex = 0 will be ignored.

I see two solutions.
The first is when initializing the BModel property to set its value to non-null, but SName = "None".
I cannot show you how to do this.
Since you haven't given enough code, I need to see how you have implemented the BListModels, emodels: SModel collection types, of an element that has a BModel property.

The second variant is to set the SelectedIndex value AFTER the ComboBox is loaded.
I'll show you an variant here using C # code, but you can also implement it using AP property, Behavior, or whatever.

<StackPanel>
    <ComboBox Width="100" VerticalAlignment="Top" 
              ItemsSource="{Binding Mode=OneWay, Source={StaticResource strings}}"
              SelectedIndex="0"
              SelectedItem="First" Loaded="ComboBox_Loaded" />
    <x:Code>
        <![CDATA[
            private void ComboBox_Loaded(object sender, RoutedEventArgs e)
            {
                ((Selector)sender).SelectedIndex = 0;
            }
        ]]>
    </x:Code>
 </StackPanel>

Upvotes: 1

Related Questions