“绑定/触发”;选择全部"-WPF中的复选框ComboBoxItem

“绑定/触发”;选择全部"-WPF中的复选框ComboBoxItem,wpf,combobox,compositecollection,Wpf,Combobox,Compositecollection,我试图创建一个WPF CustomControl复选框,除了用户定义的项目列表之外,还包含一个“全选”项目。选择“全选”时,应相应地检查列表中的所有项目。我如何处理正在单击的“全选”项目?我尝试了很多方法,但是CheckComboBox.cs中的属性“SelectAll”从未被输入 这是我当前的代码 Generic.xaml <Style TargetType="{x:Type local:CheckComboBox}"> <Setter Property="T

我试图创建一个WPF CustomControl复选框,除了用户定义的项目列表之外,还包含一个“全选”项目。选择“全选”时,应相应地检查列表中的所有项目。我如何处理正在单击的“全选”项目?我尝试了很多方法,但是CheckComboBox.cs中的属性“SelectAll”从未被输入

这是我当前的代码

Generic.xaml

<Style TargetType="{x:Type local:CheckComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CheckComboBox}">
                    <ComboBox SelectedItem="{TemplateBinding SelectedItem}"
                              SelectedValue="{TemplateBinding SelectedValue}"
                              SelectedValuePath="{TemplateBinding SelectedValuePath}"
                              DisplayMemberPath="{TemplateBinding DisplayMemberPath}"
                              IsTextSearchEnabled="{TemplateBinding IsTextSearchEnabled}"
                              ItemTemplate="{TemplateBinding ItemTemplate}"
                              x:Name="InnerComboBox" >

                        <ComboBox.Resources>
                            <ResourceDictionary>
                                <CheckBox x:Key="allItem" Content="All" IsChecked="{Binding SelectAll, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
                                <CollectionViewSource x:Key="items" Source="{Binding ComboBoxItems, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
                            </ResourceDictionary>
                        </ComboBox.Resources>

                        <ComboBox.ItemsSource>
                            <CompositeCollection>
                                <ComboBoxItem Content="{Binding Source={StaticResource allItem}}"/>
                                <CollectionContainer Collection="{Binding Source={StaticResource items}}" />
                            </CompositeCollection>
                        </ComboBox.ItemsSource>

                    </ComboBox>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding IsSelected}" Content="{Binding Text}" VerticalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

终于找到了触发“SelectAll”属性的方法。请注意:

<ComboBoxItem>
    <CheckBox ... />
</ComboBoxItem>

Generic.xaml

...
<ComboBox.Resources>
    <ResourceDictionary>
        <CollectionViewSource x:Key="items" Source="{Binding ItemsSource, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
    </ResourceDictionary>
</ComboBox.Resources>

<ComboBox.ItemsSource>
    <CompositeCollection>
        <ComboBoxItem>
            <CheckBox Content="All" IsChecked="{Binding SelectAll, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
        </ComboBoxItem>
        <CollectionContainer Collection="{Binding Source={StaticResource items}}" />
    </CompositeCollection>
</ComboBox.ItemsSource>
...
。。。
...
选中ComboBox.cs

public class CheckComboBox : ComboBox
{
    public class CheckComboBoxItem : ModelBase
    {
        public CheckComboBoxItem(bool isSelected, string text)
        {
            IsSelected = isSelected;
            Text = text;
        }

        private bool _isSelected;
        public bool IsSelected 
        { 
            get { return _isSelected; }
            set { Set(() => IsSelected, ref _isSelected, value); }
        }

        private string _text;
        public string Text 
        { 
            get { return _text; }
            set { Set(() => Text, ref _text, value); }
        }
    }

    static CheckComboBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckComboBox), new FrameworkPropertyMetadata(typeof(CheckComboBox)));
    }

    public static readonly DependencyProperty SelectAllProperty =
        DependencyProperty.Register("SelectAll",
                                    typeof (bool),
                                    typeof (CheckComboBox),
                                    new FrameworkPropertyMetadata(false,
                                        FrameworkPropertyMetadataOptions.AffectsRender,
                                        new PropertyChangedCallback(OnSelectAll)));

    private static void OnSelectAll(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        CheckComboBox checkComboBox = (CheckComboBox)d;
        IEnumerable<CheckComboBoxItem> items = (IEnumerable<CheckComboBoxItem>) checkComboBox.ItemsSource;
        foreach (var item in items)
        {
            item.IsSelected = (bool) e.NewValue;
        }
    }

    public bool SelectAll
    {
        get { return (bool) GetValue(SelectAllProperty); }
        set { SetValue(SelectAllProperty, value); }
    }
}
公共类CheckComboBox:ComboBox
{
公共类CheckComboBoxItem:ModelBase
{
public CheckComboBoxItem(bool isSelected,字符串文本)
{
IsSelected=IsSelected;
文本=文本;
}
私立学校当选;
公选学校
{ 
获取{return}isSelected;}
集合{set(()=>IsSelected,ref_IsSelected,value);}
}
私有字符串_文本;
公共字符串文本
{ 
获取{return\u text;}
集合{set(()=>Text,ref _Text,value);}
}
}
静态复选框()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckComboBox)),new FrameworkPropertyMetadata(typeof(CheckComboBox));
}
公共静态只读从属属性SelectAllProperty=
DependencyProperty.Register(“SelectAll”,
类型(bool),
类型(复选框),
新的FrameworkPropertyMetadata(错误,
FrameworkPropertyMetadataOptions.AffectsRender,
新财产变更回拨(OnSelectAll));
SelectAll上的私有静态void(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
CheckComboBox CheckComboBox=(CheckComboBox)d;
IEnumerable items=(IEnumerable)checkComboBox.ItemsSource;
foreach(项目中的var项目)
{
item.IsSelected=(bool)e.NewValue;
}
}
公共布尔SelectAll
{
获取{return(bool)GetValue(SelectAllProperty);}
set{SetValue(SelectAllProperty,value);}
}
}

现在,我只需要想一想,当另一个项目被取消选中时,如何自动取消选中“全选”复选框。

不要触摸“使用a”。这是真的。但是,即使ComboBoxItems绑定可以工作,绑定也不会触发“SelectAll”属性。
<ComboBoxItem>
    <CheckBox ... />
</ComboBoxItem>
...
<ComboBox.Resources>
    <ResourceDictionary>
        <CollectionViewSource x:Key="items" Source="{Binding ItemsSource, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
    </ResourceDictionary>
</ComboBox.Resources>

<ComboBox.ItemsSource>
    <CompositeCollection>
        <ComboBoxItem>
            <CheckBox Content="All" IsChecked="{Binding SelectAll, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" />
        </ComboBoxItem>
        <CollectionContainer Collection="{Binding Source={StaticResource items}}" />
    </CompositeCollection>
</ComboBox.ItemsSource>
...
public class CheckComboBox : ComboBox
{
    public class CheckComboBoxItem : ModelBase
    {
        public CheckComboBoxItem(bool isSelected, string text)
        {
            IsSelected = isSelected;
            Text = text;
        }

        private bool _isSelected;
        public bool IsSelected 
        { 
            get { return _isSelected; }
            set { Set(() => IsSelected, ref _isSelected, value); }
        }

        private string _text;
        public string Text 
        { 
            get { return _text; }
            set { Set(() => Text, ref _text, value); }
        }
    }

    static CheckComboBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CheckComboBox), new FrameworkPropertyMetadata(typeof(CheckComboBox)));
    }

    public static readonly DependencyProperty SelectAllProperty =
        DependencyProperty.Register("SelectAll",
                                    typeof (bool),
                                    typeof (CheckComboBox),
                                    new FrameworkPropertyMetadata(false,
                                        FrameworkPropertyMetadataOptions.AffectsRender,
                                        new PropertyChangedCallback(OnSelectAll)));

    private static void OnSelectAll(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        CheckComboBox checkComboBox = (CheckComboBox)d;
        IEnumerable<CheckComboBoxItem> items = (IEnumerable<CheckComboBoxItem>) checkComboBox.ItemsSource;
        foreach (var item in items)
        {
            item.IsSelected = (bool) e.NewValue;
        }
    }

    public bool SelectAll
    {
        get { return (bool) GetValue(SelectAllProperty); }
        set { SetValue(SelectAllProperty, value); }
    }
}