我有一个Style
for DataGridCell
(只有触发器很重要。)
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Foreground" Value="{StaticResource ForegroundBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" SnapsToDevicePixels="True" >
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Margin="4, 0, 0, 0"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="{StaticResource DarkForegroundBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
当我像这样定义列时它起作用:
<DataGridTemplateColumn Header="Column1" Width="Auto" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Property1, Mode=OneWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
但是当我有TextBox
而不是TextBlock
这样的时候:
<DataGridTemplateColumn Width="Auto" Header="Column1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Property1, Mode=OneWay}" IsReadOnly="True" TextWrapping="Wrap" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
它不起作用,因为TextBox
它有它自己的Style
. 我需要使用TextBox
,因为我想让用户从单元格中选择文本。但我也需要在选择单元格/行时Foreground
更改颜色。
(Background color is dark and foreground color is light, but when a cell/row is selected then the background color is light and foreground color should be dark)
编辑
我编辑了我的问题以使其更清楚。对之前的误解深表歉意。我的目标是拥有TextBox
并DataGridCell
使用Trigger
s from DataGridCellStyle
。
任何帮助表示赞赏。