【问题标题】:Datagrid Template Column Cell Style Trigger not workingDatagrid模板列单元格样式触发器不起作用
【发布时间】:2021-12-29 04:46:20
【问题描述】:

我有一个数据网格模板列,我在其中更改文本块“MyTextBlockRotationVersion”后面代码中的文本颜色。我正在尝试使用文本颜色更改来更改单元格的背景颜色,以便当它从单元格模板变为单元格编辑模板时,反之亦然,红色背景仍然存在。不知道为什么触发器没有将背景变为红色。现在文本颜色会改变,但背景不会。在进入单元编辑模板并返回单元模板后,我失去了颜色变化。

                <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>

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    我认为您无法从另一个 DataTemplate(或 ControlTemplate)中访问元素

    还可以通过您要使用SourceName 而不是x:Name 的名称访问特定元素(x:Name 命名触发器)

    【讨论】:

      猜你喜欢
      • 2011-09-09
      • 2012-03-08
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      相关资源
      最近更新 更多