首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选中所有复选框WPF

选中所有复选框WPF
EN

Stack Overflow用户
提问于 2017-07-18 05:34:21
回答 1查看 5K关注 0票数 5

我想通过选择带有“以上所有”的复选框名称来使所有复选框都被选中。复选框在列表框中。

代码语言:javascript
复制
<ListBox SelectionMode="Multiple" 
         BorderThickness="0" 
         ItemsSource="{Binding QuestionThreeSelection}" 
         SelectedItem="{Binding QuestionThreeSelection}" 
         Name="listBoxList" 
         SelectionChanged="listBoxList_SelectionChanged">
    <ListBox.InputBindings>
        <KeyBinding Command="ApplicationCommands.SelectAll"
                    Modifiers="Ctrl"
                    Key="A" />
    </ListBox.InputBindings>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Checked="CheckBox_Checked_1"   
                      Content="{Binding SourceName}" 
                      IsChecked="{Binding Path=IsSelected,  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

反码

代码语言:javascript
复制
private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
{          
    var oo = listBoxList;
    CheckBox cb = (CheckBox)sender;
    //var w=e;

    IEnumerable<AddSource> listallityem = ((IEnumerable<AddSource>)listBoxList.Items.SourceCollection).Where(r => r.IsSelected == false);
    //IEnumerable<AddSource> listallityem1 = ((IEnumerable<AddSource>)listBoxList.Items.SourceCollection);

    AddSource vv = cb.DataContext as AddSource;
    if ((bool) cb.IsChecked)
    {

    }

    if (vv.SourceName== "All of the above")
    {
        r = listBoxList.ItemsSource;

        foreach (AddSource item in wer)
        {
            item.IsSelected = true; // false in case of unselect
        }
    }
}

有人能建议一个方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-18 10:02:06

您可以为“以上所有”的CheckedUnchecked事件处理如下所示的CheckBox

代码语言:javascript
复制
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    SelectAll(true);
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    SelectAll(false);
}

private void SelectAll(bool select)
{
    var all = listBoxList.ItemsSource as IEnumerable<AddSource>;
    if (all != null)
    {
        foreach (var source in all)
            source.IsSelected = select;
    }
}

确保AddSource类实现了INotifyPropertyChanged,并在IsSelected属性的setter中引发了PropertyChanged事件。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45158314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档