DatagridCell样式由TextBox样式覆盖 [英] DatagridCell style overriden by TextBox style
问题描述
我有一个 Style
for DataGridCell
(只有触发器很重要。)
I have a Style
for DataGridCell
(only triggers are important.)
<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>
当我定义列时,它可以工作:
It works when I define column like this:
<DataGridTemplateColumn Header="Column1" Width="Auto" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Property1, Mode=OneWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
但是当我有 TextBox
而不是 TextBlock
像这样:
But when I have TextBox
instead of TextBlock
like this:
<DataGridTemplateColumn Width="Auto" Header="Column1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Property1, Mode=OneWay}" IsReadOnly="True" TextWrapping="Wrap" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
它不起作用,因为 TextBox
它是自己的样式
。
我需要使用 TextBox
,因为我想让用户从单元格中选择文本。但是,当选择单元格/行时,我还需要前景
颜色来更改颜色。
It doesn't work since TextBox
has it's own Style
.
I need to use TextBox
because I want to let the user select the text from cell. But also I need the Foreground
color to change when the cell/row is selected.
(背景颜色较暗,前景色较浅,但选择单元格/行时,背景颜色为浅色前景色应该是黑暗的)
编辑
我编辑我的问题要更清楚。对不起,以前的误会。我的目标是在 DataGridCell
中使用 TextBox
,并使用触发器
s $ DataGridCellStyle
。
I edited my question to be more clear. Sorry for previous misunderstandings. My goal here is to have TextBox
in DataGridCell
and use Trigger
s from DataGridCellStyle
.
任何帮助赞赏。
推荐答案
不是离开/取TextBox / Textbox风格。
It's not leaving/taking the Textblock/Textbox style instead. You put a textbox in there and a textbox has a background.
尝试将TextBox背景设置为透明。我还建议删除文本框边框,如果你想要的是从单元格中选择文本。
Try setting the TextBox background to transparent. I'd also suggest removing the textbox borders as well if all you want is to select the text from the cell.
您可以设置以下属性来查看你
You can set the following properties to get the look you want.
背景=透明BorderThickness =0IsReadOnly =True
设置TextBox上的前景
To set the foreground on the TextBox
<DataTemplate>
<TextBox Name="Display" Text=.../>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding
RelativeSource={RelativeSource
Mode=FindAncestor,AncestorType={x:Type DataGridCell}},Path=IsSelected}" Value="true">
<Setter TargetName="Display" Property="Foreground">
<Setter.Value>
<SolidColorBrush Color="{StaticResource DarkForegroundBrush}"/>
</Setter.Value>
</Setter>
</DataTrigger>
</DataTemplate.Triggers/>
</DataTemplate>
这篇关于DatagridCell样式由TextBox样式覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!