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>
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>
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.
(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)
EDIT
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
.
Any help appreciated.