Search code examples
c#wpfxamldatagridcomponentone

Apply behaviour to WPF DataGrid Row


I'd like to attach a behaviour to a (Component One) data grid row.

The problem is: I can't get to the DataContext of the actual row. In the style my data context is the data grid.. how can I get to the row's data context?

<c1:C1DataGrid.RowStyle>
    <Style TargetType="c1:DataGridRowPresenter">
            <Setter Property="ui:DataGridRowHierarchyBehavior.IsExpanded" Value="{Binding IsExpanded, RelativeSource=}" />
    </Style>
</c1:C1DataGrid.RowStyle>

I know about how to 'get up' the visual tree using RelativeSource -
though how 'getting down' could work I don't have a clue..

Any hints much appreciated!


Solution

  • To get to the DataItem of the row, you need to use RelativeSource in the Binding in the following way :

    <c1:C1DataGrid.RowStyle>
       <Style TargetType="{x:Type c1:DataGridRowPresenter}">
          <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=Row.DataItem.Background}"></Setter>
       </Style>
    </c1:C1DataGrid.RowStyle>
    

    I've used the Background property as an example.