简体   繁体   中英

DataGridCell style based on DataGrid property

I want to add a style to the DataGridCell of a custom DataGrid which is based on property of that DataGrid . For example I want to have a DependencyProperty named VisualizeReadOnlyCells and if that property is set to True, I want the DataGridCell that is ReadOnly is drawn with gray background. And other similar things. As an workaround I have created two different styles for the DataGridCell and I assign those styles to the CellStyle property of the DataGrid in the DataGrid style definition. But with more properties the number of such styles will grow and the code would be very unmanageable.

My current solution is:

<Style x:Key="DataGridCellNoVisualize" TargetType="DataGridCell">
    - style definition
</Style>

<Style x:Key="DataGridCellVisualize" TargetType="DataGridCell">
    <Style.Triggers>
        <Trigger Property="IsReadOnly" Value="True">
            <Setter Property="Background" Value="Gray"/>
        </Trigger>
    </Style.Triggers>
</Style>

<Style x:Key="DataGridStyle" TargetType="MyCustomDataGrid">
    <Setter Property="CellStyle" Value="{StaticResource DataGridCellNoVisualize"/>
    <Style.Triggers>
        <Trigger Property="VisualizeReadOnlyCells" Value="True">
            <Setter Property="CellStyle" Value="{Static Resource DataGridCellVisualize"/>
        </Trigger>
    </Style.Triggers>
</Style>

What I would like to get is something like this:

<Style x:Key="DataGridCellUniversalStyle" TargetType="DataGridCell">
    - style definition
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsReadOnly" Value="True"/>
                <Condition Property="MyParentDataGrid.VisualizeReadOnlyCells" Value="True"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Background" Value="Gray"/>
            </MultiTrigger.Setters>
        </MultiTrigger>
    </Style.Triggers>
</Style>

If I understand your issue correctly, you could use a MultiDataTrigger and bind to the DataGridCell and the parent DataGrid :

<Style x:Key="DataGridCellUniversalStyle" TargetType="DataGridCell">
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True"/>
                <Condition Binding="{Binding VisualizeReadOnlyCells, RelativeSource={RelativeSource AncestorType=local:MyCustomDataGrid}}" Value="True"/>
            </MultiDataTrigger.Conditions>
            <MultiDataTrigger.Setters>
                <Setter Property="Background" Value="Gray"/>
            </MultiDataTrigger.Setters>
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM