wpf 数据网格标题中的访问复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14732096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:23:14  来源:igfitidea点击:

Access checkbox in wpf datagrid header

c#wpfdatagrid

提问by hs2d

Hei,

嘿,

I need help figuring out how to access checkbox in wpf datagrid header. Here's what i have:

我需要帮助弄清楚如何访问 wpf datagrid 标头中的复选框。这是我所拥有的:

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

I have set the Namefor the checkbox, but for some reason i cant access it from on the code side.

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

I need to access the checkbox to uncheck it after i refresh my datagrid items. How can i do this?

刷新数据网格项目后,我需要访问复选框以取消选中它。我怎样才能做到这一点?

采纳答案by Eirik

Allthough binding might be the way you should go, it is possible to do what you ask. Here's one way of doing it:

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

1.Give your header CheckBoxan Uid

1.给你的标题CheckBox一个Uid

<CheckBox Uid="CheckAll" />

2.Name your DataGrid

2.命名你的 DataGrid

<DataGrid Name="myDataGrid" />

3.Implement the following extension method

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.Access and uncheck the CheckBoxin code behind like this

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

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

回答by JoanComasFdz

A working example in MVVM:

MVVM 中的一个工作示例:

ViewModel

视图模型

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

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>

回答by Darlan Dieterich

Set one simple event:

设置一个简单的事件:

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

In the event Click on the code:

在事件中点击代码:

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

NOTE: The sender is checkbox

注意:发件人是复选框