I am looking for way to select the first item when data became available. But if no data in the source , then do not select. How to do it ? I am very new to WPF.
<ComboBox Grid.Row="5" Grid.Column="1"
IsEditable="False"
ItemsSource="{Binding Source={x:Static l:DirectXResolution.Resolutions}}"
ToolTip="Resolutions">
<ComboBox.Resources>
<l:ResolutionConverter x:Key="resolutionConverter"/>
</ComboBox.Resources>
<ComboBox.Text>
<MultiBinding Converter="{StaticResource resolutionConverter}">
<Binding Path="GameWidth" Mode="OneWayToSource"/>
<Binding Path="GameHeight" Mode="OneWayToSource"/>
</MultiBinding>
</ComboBox.Text>
</ComboBox>
The easiest way is to use SelectedIndex. Please, check the code below.
<ComboBox Grid.Row="5" Grid.Column="1"
IsEditable="False"
ItemsSource="{Binding Source={x:Static l:DirectXResolution.Resolutions}}"
ToolTip="Resolutions"
SelectedIndex="0">
....
DirectXResolution.Resolutions
must be ObservableCollection<T>
otherwise your ComboBox
will not be updated when the data becomes available. You can use CollectionChanged
event of ObservableCollection<T>
to select the first item.
If DirectXResolution.Resolutions
is not ObservableCollection
, create a wrapper for this collection and inherit INotifyCollectionChanged
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With