【问题标题】:Access checkbox in wpf datagrid headerwpf datagrid标题中的访问复选框
【发布时间】:2013-02-06 15:01:56
【问题描述】:

嘿,

我需要帮助弄清楚如何访问 wpf 数据网格标题中的复选框。这是我所拥有的:

<DataGrid.Columns>
    <DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
        <DataGridTemplateColumn.HeaderTemplate>
            <DataTemplate>
                <CheckBox Name="cbxAll" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </DataTemplate>
        </DataGridTemplateColumn.HeaderTemplate>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=NoErrors}" Name="theCheckbox" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

我已经为复选框设置了Name,但由于某种原因,我无法从代码端访问它。

刷新我的数据网格项目后,我需要访问该复选框以取消选中它。我该怎么做?

【问题讨论】:

  • 您不需要“访问”复选框。 WPF不是winforms。 UI is not Data。将 IsChecked 属性绑定到基础 DataContext 中的布尔值。学习 MVVM。
  • 另外,如果仔细观察,CheckBox 位于DataTemplate 内,这意味着没有实际的 CheckBox。它只是一个模板,因此,虽然有一种方法可以在运行时检查可视化树并获取对它的引用,但这并不是实现您想要在此处实现的目标的方法。
  • 第二次学习 MVVM。如果不先了解 WPF,几乎不可能在 WPF 中做任何事情。
  • 请注意,我需要更改的复选框位于数据网格标题中。绑定 IsChecked 将如何帮助我?请举个例子?

标签: c# wpf datagrid


【解决方案1】:

MVVM 中的一个工作示例:

视图模型

public class MainWindowViewModel : INotifyPropertyChanged
{
    private bool allItemsAreChecked;

    public event PropertyChangedEventHandler PropertyChanged;

    public bool AllItemsAreChecked
    {
        get
        {
            return this.allItemsAreChecked;
        }
        set
        {
            this.allItemsAreChecked = value;
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs("AllItemsAreChecked"));
            }
        }
    }
}

XAML

<DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
    <DataGridTemplateColumn.HeaderTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding
                RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                Path=DataContext.AllItemsAreChecked}" />
        </DataTemplate>
    </DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>

【讨论】:

  • 这实际上为我解决了很多问题。例如我能够使用它解决全选复选框功能,包括它的中间状态。您需要做的就是将值设置为布尔属性,如您的示例中的 AllItemsAreChecked。而且我想,这是更好、更精确的方法,而不是在运行时进行破解。
  • 对于当前 WPF 版本,绑定应更新为:IsChecked="{Binding Path=DataContext.AllItemsAreChecked, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor }}"
【解决方案2】:

尽管绑定可能是您应该采用的方式,但您可以按照您的要求去做。 这是一种方法:

1.给你的标题CheckBox一个Uid

<CheckBox Uid="CheckAll" />

2.命名你的DataGrid

<DataGrid Name="myDataGrid" />

3.实现如下扩展方法

public static UIElement FindUid(this DependencyObject parent, string uid)
{
    var count = VisualTreeHelper.GetChildrenCount(parent);
    if (count == 0) return null;

    for (int i = 0; i < count; i++)
    {
        var el = VisualTreeHelper.GetChild(parent, i) as UIElement;
        if (el == null) continue;

        if (el.Uid == uid) return el;

        el = el.FindUid(uid);
        if (el != null) return el;
    }
    return null;
}

4.访问并取消选中后面代码中的CheckBox

CheckBox checkBox = myDataGrid.FindUid("CheckAll") as CheckBox;
checkBox.IsChecked = false;

【讨论】:

  • 感谢您的工作示例!比仅仅说“学习 MVVM”要好。我知道这种方法不是最好的,但是由于没有人提供如何在标题中绑定复选框的示例,所以这是我唯一的选择。再次感谢!
【解决方案3】:

设置一个简单的事件:

<CheckBox x:Name="cbxAll" Click="cbxAll_Click"/>

在事件中点击代码:

private void cbxAll_Click(object sender, RoutedEventArgs e)
{
   var ckbox = sender as CheckBox;
   if (ckbox.IsChecked == true)
   {
    //Check all itens
   }
   else
   {
    //Uncheck all itens
   }
}

注意:发件人是复选框

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 2018-06-24
    • 2011-10-14
    • 1970-01-01
    • 2016-01-13
    相关资源
    最近更新 更多