如何从wpf中的combobox中获取已检查的项目?

时间:2016-12-13 05:51:37

标签: wpf

我有一个带有复选框和项目的组合框,如何在组合框中选中时选择特定项目或多项目..需要帮助..下面我尝试了代码..

     <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>

1 个答案:

答案 0 :(得分:1)

1)在模型中添加Bool类型IsSelected属性。并将该属性绑定到Template中Checkbox的IsChecked属性。 2)将该属性绑定到ItemContainerStyle中的ComboBoxItem的IsSelected。  您可能想要提出其他解决方案以使Check All工作(如转换器)。 这将有效。