0

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>
Arvangen
  • 127
  • 2
  • 12
  • Create a property on your collection item ViewModel and bind the `IsChecked` property to the ViewModel. – nemesv Aug 19 '12 at 20:52
  • @nemesv I dont understand how can I do that. There are many strings in MyCollection. I want for example to check checkbox which have string "aaa" in content. When I create property in ViewModel and bind IsChecked property, than all checkboxes IsChecked property will be conected with property in ViewModel. Am I right? – Arvangen Aug 19 '12 at 21:00

1 Answers1

1

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

TMan
  • 4,044
  • 18
  • 63
  • 117
  • I have strings in MyCollection which is ItemSource in ComboBox. You mean that I should have collection of some kind of objects with string property and boolean IsChecked property? – Arvangen Aug 19 '12 at 21:08
  • Yes it would be possible to do it that way but would be rather dirty to go that route. You could also possible make a converter and based on the value passed in it will return true or false whether to check the box or not. You follow this link that will show the idea of creating converters: http://stackoverflow.com/questions/11938864/disable-datagridcell-if-another-cell-is-being-used – TMan Aug 19 '12 at 23:58