DatagridCell style overriden by TextBox style

2019-08-02 09:01发布

问题:

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 Triggers from DataGridCellStyle.

Any help appreciated.

回答1:

It's not leaving/taking the Textblock/Textbox style instead. You put a textbox in there and a textbox has a background.

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.

Background="Transparent" BorderThickness="0" IsReadOnly="True"

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>