I have combobox with checkbox in data template. Combobox ItemSource property is binded with collection in ViewModel. I want to make one particular checkbox checked for default. How can I do this?
<ComboBox Grid.Column="1"
ItemsSource="{Binding MyCollection, Mode=OneWay}"
Style="{StaticResource MyComboboxStyle}"
Margin="5"
MinWidth="120">
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=DataContext.MyCheckedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Content="{Binding}"
IsChecked="false"
VerticalAlignment="Center"
Margin="3"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
I would make a boolean property in your viewmodel then upon loading of collection, find the object in your collection that should be checked and set if to true.
public bool IsChecked { get; set; }
XAML:
<CheckBox Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=DataContext.MyCheckedCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
Content="{Binding}"
IsChecked="{Binding IsChecked}"
VerticalAlignment="Center"
Margin="3"/>
However this may require you to have this property apart of you object model