Manjunath ST
Manjunath ST

Reputation: 119

How to get the checked items from combobox in wpf?

I have a combobox with checkbox and items, How do i select the particular item or multi item when checked in combobox.. Need help.. Below i tried code..

     <UserControl.Resources>
    <DataTemplate x:Key="cmbIndex">
        <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"
                  Content="{Binding}"
                  Click="CheckBox_Click">

        </CheckBox>
    </DataTemplate>
    <CollectionViewSource x:Key="coll" Source="{Binding CMDCollection}"/>
 <UserControl.Resources>
  <ComboBox Grid.Row="0" 
                                  HorizontalAlignment="Left" Margin="80,0,0,0" 
                                   SelectedItem="{Binding T3Command}" 
                                  Height="20" VerticalAlignment="Center" Width="60" 
                                  FontFamily="Calibri" FontSize="12">
                        <ComboBox.ItemsSource>
                            <CompositeCollection>
                                <ComboBoxItem>
                                    <CheckBox x:Name="all">Select All</CheckBox>
                                </ComboBoxItem>
                                <CollectionContainer   Collection="{Binding Source={StaticResource coll}}"/>
                            </CompositeCollection>
                        </ComboBox.ItemsSource>
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <CheckBox Name="chkTask" Content="{Binding}" IsChecked="{Binding ElementName=all, Path=IsChecked, Mode=OneWay}"></CheckBox>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                        <ComboBox.Style>
                            <Style TargetType="{x:Type ComboBox}">
                              <Setter Property="ItemTemplate" Value="{StaticResource cmbIndex}"/>
                            </Style>
                        </ComboBox.Style>
                    </ComboBox>

Upvotes: 0

Views: 330

Answers (1)

WPFUser
WPFUser

Reputation: 1175

1) Add a Bool type IsSelected property in your model. and bind that property to IsChecked property of Checkbox in Template. 2) Bind that property to ComboBoxItem's IsSelected in ItemContainerStyle. you might want to come up with other solution to make Check All working(like converters). this will work.

Upvotes: 1

Related Questions