IronHide
IronHide

Reputation: 376

How to access element defined in DataGrid RowHeaderTemplate or DataGrid.RowHeaderSyle

I the following code

<DataGrid.RowHeaderTemplate >
    <DataTemplate>
        <CheckBox x:Name="SelectedItemCheckBox"
                    Margin="5 0 0 0"
                    IsChecked="{Binding Path=IsSelected,
                                        Mode=TwoWay,
                                        RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGridRow}}}">
        </CheckBox>
    </DataTemplate>
</DataGrid.RowHeaderTemplate>

or

<DataGrid.RowHeaderStyle>
    <Style TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Background" Value="White"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRowHeader}">
                    <CheckBox x:Name="SelectedItemCheckBox"
                                Margin="5 0 0 0"
                                IsChecked="{Binding Path=IsSelected,
                                                    Mode=TwoWay,
                                                    RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGridRow}}}">
                    </CheckBox>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.RowHeaderStyle>

How can I access the SelectedItemCheckBox from code behind when row is selected ?

What I have so far:

private CheckBox GetCheckbox(int index)
{
    DataGridRow row = (DataGridRow)MyGrid.ItemContainerGenerator.ContainerFromIndex(index);

    //how do I get to that checkbox here ?
}

The ItemSource of MyGrid is set in code behind, normally I would access the cell by accessing MyGrid.Columns[] however this is a row header and it's not part of Columns[].

Please note that there are many rows with this checkbox defined depending the ItemSource size.

Also I wold like to know if there is a way of accessing the checkbox without changing the xaml and using it as it is.

Upvotes: 0

Views: 219

Answers (1)

Mikael Koskinen
Mikael Koskinen

Reputation: 12906

If you want to access the row header's checkbox in your code-behind (and not use binding), you can "travel" the visual tree of your selected DataGridRow to find the header.

Add SelectionChanged event handler to the DataGrid:

<DataGrid x:Name="Grid" Loaded="Grid_Loaded" SelectionChanged="Grid_SelectionChanged">

Then in code-behind:

  1. Get the selected row
  2. Use VisualTreeHelper to find the header's checkbox
  3. Do your magic

    private void Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var item = (DataGridRow)Grid.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);
    
        var control = FindChild<CheckBox>(item, "SelectedItemCheckBox");
    
        control.IsChecked = true;
    }
    

For FindChild, there's multiple options available in here: How can I find WPF controls by name or type?

I used the following in this example: How can I find WPF controls by name or type?

public static T FindChild<T>(DependencyObject depObj, string childName)
   where T : DependencyObject
{
    // Confirm obj is valid. 
    if (depObj == null) return null;

    // success case
    if (depObj is T && ((FrameworkElement)depObj).Name == childName)
        return depObj as T;

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);

        //DFS
        T obj = FindChild<T>(child, childName);

        if (obj != null)
            return obj;
    }

    return null;
}

Upvotes: 1

Related Questions