Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access checkbox in wpf datagrid header

Tags:

c#

wpf

datagrid

Hei,

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

<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 Name for the checkbox, but for some reason i cant access it from on the code side.

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

like image 391
hs2d Avatar asked Feb 06 '13 15:02

hs2d


2 Answers

A working example in 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

<DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
    <DataGridTemplateColumn.HeaderTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding
                RelativeSource={RelativeSource AncestorType={x:Type DataGrid}},
                Path=DataContext.AllItemsAreChecked}" />
        </DataTemplate>
    </DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
like image 140
JoanComasFdz Avatar answered Nov 15 '22 05:11

JoanComasFdz


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 CheckBox an Uid

<CheckBox Uid="CheckAll" />

2.Name your DataGrid

<DataGrid Name="myDataGrid" />

3.Implement the following extension method

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 CheckBox in code behind like this

CheckBox checkBox = myDataGrid.FindUid("CheckAll") as CheckBox;
checkBox.IsChecked = false;
like image 28
Eirik Avatar answered Nov 15 '22 05:11

Eirik