c# - MVVM 如何实现一个 "Check All"复选框

标签 c# wpf xaml mvvm checkbox

我有许多复选框代表将在数据网格中显示或隐藏的列。截至目前,除了全选复选框外,它们都可以使用。选中后,它应该如名称所示选中组内的所有复选框。

这些复选框都与 View 模型无关,它们只影响数据在 View 本身上的外观。一种解决方案是在 ViewModel 上实现所有复选框,然后执行 Check All 命令并将所有标志设置为 true。我只是不喜欢这个想法,因为我添加了 20 个属性和各种命令等,所有这些都只是为了支持这一件事。

我有一些可能使用“IsThreeState”属性的想法,但没有凝聚力。我也不反对将它们呈现在不同的容器中,我只是不确定它是否有帮助。

<Expander  DockPanel.Dock="Top" Header="{x:Static p:Resources.shSettings}" IsExpanded="True">
            <DockPanel> 
                <GroupBox Header="Filter Columns" DockPanel.Dock="Right">
                    <ag:AutoGrid Columns="150, 150, 150" RowCount="7" RowHeight="20">
                        <CheckBox Content="Check All" />
                        <CheckBox Content="Co. Name" x:Name="filterCoName" />
                        <CheckBox Content="Rating" x:Name="filterRating" />

                        <CheckBox Content="Severity (!)" x:Name="filterSeverity" />
                        <CheckBox Content="Stn ID" x:Name="filterStation" />
                        <CheckBox Content="MB" x:Name="filterMB" />

                        <CheckBox Content="New" x:Name="filterNew" />
                        <CheckBox Content="KV" x:Name="filterKV" />
                        <CheckBox Content="STE Rating" x:Name="filterSiteRating" />

                        <CheckBox Content="QCA" x:Name="filterQCA" />
                        <CheckBox Content="Area" x:Name="filterArea" />
                        <CheckBox x:Name="filterDuration" Content="Duration" />

                        <CheckBox Content="Plan in Place" x:Name="filterPIP"  />
                        <CheckBox Content="Type" x:Name="filterType" />
                        <CheckBox Content="Rating(%)" x:Name="filterRatingPercent" />

                        <CheckBox Content="ID" x:Name="filterId" />
                        <CheckBox Content="Pre" x:Name="filterPre" />
                        <CheckBox Content="Deviation" x:Name="filterDeviation" />

                        <CheckBox Content="Status" x:Name="filterStatus" />
                        <CheckBox Content="Post" x:Name="filterPost" />
                        <CheckBox Content="Description" x:Name="filterDescription" />
                    </ag:AutoGrid>
                </GroupBox>
            </DockPanel>
        </Expander>

最佳答案

如果不使用 viewmodel,你能不能在代码隐藏中这样做?

将事件处理程序附加到选中所有复选框选中状态更改事件。只要您的控件都已命名,就可以从代码后面与它们进行交互。

关于c# - MVVM 如何实现一个 "Check All"复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25491046/

相关文章:

c# - 0 - 65535 之间第一个内存地址的空引用

c# - Opera 后退按钮缓存功能

c# - 绑定(bind) DataTemplate 中的 RotateTransform Angle 未生效

WPF 组合框 Entity Framework 绑定(bind) (MVVM)

wpf - 如何获取 UIElement 的父级?

xaml - 如何将 xaml Storyboard绑定(bind)到 WP7 中自定义控件的 Point 属性?

c# - 使用系统时钟测量广播消息延迟,好主意?

c# - 如何针对 WPF 编写 specflow

c# - 2 个 XAML 文件之间的数据绑定(bind)

wpf - 如何在没有事件处理程序的情况下将焦点设置到 TextBox