I have WPF application and use UserControl as a view. Inside that UserControl there is DevExpress ComboBoxEdit.
<UserControl ...
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<dxe:ComboBoxEdit Name="ComboBoxInspectionList" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding InspectionList}" SelectedItem="{Binding SelectedInspection}" IsTextEditable="False"/>
</Grid>
</UserControl>
ComboBox is data bound. I've tried this:
public partial class InspectionList : UserControl
{
public InspectionList()
{
InitializeComponent();
if (ComboBoxInspectionList.Items.Count > 0)
{
ComboBoxInspectionList.SelectedIndex = 0;
}
}
}
But, data binding happened after the code in UserControl's constructor is executed.
Can't you just set the SelectedIndex
property to 0 in the XAML markup?:
<dxe:ComboBoxEdit Name="ComboBoxInspectionList" SelectedIndex="0" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding InspectionList}" SelectedItem="{Binding SelectedInspection}" IsTextEditable="False"/>
The MVVM way of doing this would be to set the SelectedInspection
property to an object that is actually present in the InspectionList
in the view model as suggested by @3615:
SelectedInspection = InspectionList.FirstOrDefault();
You can obviously not select an item that is not present in the Items
collection of the ComboBox
.