简体   繁体   中英

Datagrid Template Column Cell Style Trigger not working

I have a datagrid template column where I am changing the text color from code behind for textblock "MyTextBlockRotationVersion". I am trying to use the text color change to also change the background color of the cell so that when it goes from celltemplate to celleditingtemplate and vice versa the red background remains. Not sure why the trigger isn't turning the background red. Right now the text color changes but the background does not. I lose the color changes after it goes to celleditingtemplate and back to celltemplate.

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>

                        <TextBlock x:Name="MyTextBlockRotationVersion" Text="{Binding RotationVersion, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextWrapping="Wrap" VerticalAlignment="Center" HorizontalAlignment="Center" MaxWidth="70"/>

                    </DataTemplate>
                    
                </DataGridTemplateColumn.CellTemplate>

                <DataGridTemplateColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">

                                <Setter Property="IsEditing" Value="True" />

                            </Trigger>
                            <Trigger x:Name="MyTextBlockRotationVersion" Property="Foreground" Value="Red">

                                <Setter Property="Background" Value="Red"/>

                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTemplateColumn.CellStyle>

I don't think you can access the elements from within another DataTemplate (or ControlTemplate )

Also to access a specific element by name you want to use SourceName and not x:Name ( x:Name names the trigger)

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