0
votes

I have TextBlock in the DataGridTemplateColumn in WPF Datagrid. when I check "IsEnable" false to inherit the Textblock Style inside the DatagridTemplateColumn. Here is XAML code i'm using:

    <Style TargetType="{x:Type DataGrid}" >
        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGrid }">
                    <ControlTemplate.Resources >
                        <Style TargetType="{x:Type TextBlock }">
                            <Style.Triggers>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>

                    </ControlTemplate.Resources>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

This did not work and later i tried:

         <Style  TargetType="TextBlock" >
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}" />                    
            </Trigger>
        </Style.Triggers>
    </Style>

Any Thoughts on how to check if the Texblock inside the Datagrid "IsEnabled" and inherit the Style?.

2
tbh I can not understand what you are looking for please try to explain your requirementMuds
do you want to change the color of your textblock when its isEnabled = false ?Muds

2 Answers

0
votes

WPF does not apply implicit styles inside templates unless the TargetType derives from Control. Since TextBlock doesn't derive from Control, its style is not applied. So you either have to manually apply the style to every non-Control or define the implicit style inside the template. Define your styles inside datagrid resources as

       <DataGrid.Resources>
             <Style TargetType="{x:Type TextBlock}">
               <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                       <Setter Property="Foreground" Value="Red"/>
                     </Trigger>
                </Style.Triggers>
              </Style>
        </DataGrid.Resources>        
0
votes

I'm going to assume you are trying to toggle the foreground color based on the IsEnabled state of the TextBlock.

Where are you setting the IsEnabled = true Foreground color? You have not given the code for the actual TextBlock you are going to style.

Try this:

<Style TargetType="{x:Type TextBlock}">
   <Style.Triggers>
     <Trigger Property="IsEnabled" Value="True">
       <Setter Property="Foreground" Value="Red" />
     </Trigger>
     <Trigger Property="IsEnabled" Value="False">
       <Setter Property="Foreground" Value="Green" />
     </Trigger>
   </Style.Triggers>
</Style>

If this doesn't work, it means wherever your Textblock is defined, you are doing this -

<TextBlock .... Foreground="SomeColor" />

and you need to remove the Foreground setting directly on the TextBlock so that the Foreground color can be set by the style.