Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a style to a property of whatever datacontext has the styled element?

I have a datatrigger inside a style trigger like the following one

<Style TargetType="DataGridCell" x:Key="ChangeAnimation" >
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=APropertyOfTheDataGridCellContext Bind DataContext}" Value="True" >
            ...
        </DataTrigger>
    </Style.Triggers>
</Style>

I will apply this style to some DataGridColumns using the property CellStyle. What I would like is to bind the DataTrigger of the style to a property of the datacontext of the cell, this way I will be able to use this Style on all of the required columns instead of having to create a new Style for each column. Is this possible?

Thanks in advance.

EDIT: To clarify, the DataContext of the DataGrid is a ViewModel with a property called Rows and the Rows has a property called Cells with a property called Blink. I want to reference the property inside the style like this:

<DataGrid.Resources>
<Style TargetType="DataGridCell" x:Key="ChangeAnimation" >
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Blink}" Value="True" >
            ...
        </DataTrigger>
    </Style.Triggers>
</Style>
</DataGrid.Resources>

This gives me an error aws the DataGridResources datacontext is the one containing the Rows not the cells one. How can I reference the DataGridCell DataContext from the style?

like image 589
Ignacio Soler Garcia Avatar asked Oct 23 '25 15:10

Ignacio Soler Garcia


1 Answers

Removing The x:Key attribute then that will be appliced to all the columns.

<Style TargetType="{x:Type DataGridCell}"> 
    <Style.Triggers>         
        <DataTrigger Binding="{Binding Path=DataContext.MyProperty,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type DataGridCell}}}" Value="True" />                       
   </Style.Triggers>
</Style>

If you have 2 DataGrids then you should keep this style in the Resources tag of the dataGrid you want to modify ...

If RelativeSource is not working i would prefer to change the Template of the Column by a trigger to achive a similar behavior

Writing Trigger for the Column

<WpfToolkit:DataGridTemplateColumn MinWidth="50"
                                   MaxWidth="80"
                                   Header="MyHeader"
                                   IsReadOnly="True">
    <WpfToolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <DataTemplate.Resources>
                <DataTemplate x:Key="normalTemplate" />
                <DataTemplate x:Key="ChangeTempalte">
                   <!-Your Controls->
                </DataTemplate>
            </DataTemplate.Resources>
            <ContentPresenter x:Name="chnagedField"
                              Content="{Binding}"
                              ContentTemplate="{StaticResource ResourceKey=normalTemplate}" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=MyProperty}" Value="True">
                    <Setter TargetName="EditableField" Property="ContentTemplate" Value="{StaticResource ResourceKey=chnageTempalte}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </WpfToolkit:DataGridTemplateColumn.CellTemplate>
</WpfToolkit:DataGridTemplateColumn>  

Here MyProperty is the Property of the DataContext of the DataGridRow so you can chose what ever level of nested property yo Compare for that specific row... only downside is that you have to do this for all the column that require this

I hope it helps

like image 59
Ankesh Avatar answered Oct 26 '25 06:10

Ankesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!