DataGrid Selected Cell Background

谁说我不能喝 提交于 2019-12-11 05:16:44

问题


I'm having trouble with the WPF DataGrid.

I have the following code..

<Style TargetType="{x:Type DataGridCell}">
       <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}" />
       <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
            <Border Name="DataGridCellBorder">
            <ContentControl Content="{TemplateBinding Content}">
             <ContentControl.ContentTemplate>
                <DataTemplate>
                   <TextBlock
                      Width="auto"
                      Height="auto"
                      Background="Transparent"
                      Text="{Binding Text}"
                      TextTrimming="CharacterEllipsis"
                      TextWrapping="WrapWithOverflow" />
                        </DataTemplate>
                     </ContentControl.ContentTemplate>
                  </ContentControl>
               </Border>
               <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Orange" />
                </Trigger>
               </ControlTemplate.Triggers>
            </ControlTemplate>
          </Setter.Value>
       </Setter>
    </Style>

As you can see, I have replaced the default DataGridCell with a custom template. This was needed as I wanted the cells to have TextTrimming if the text was too big to fit in to the given cell (this is fully shown in the ToolTip when the user hovers over a given cell)

My problem now is that when I select a cell, the foreground get's set to White no matter what I try and do - What I really want to happen is have the cells (or even better, the complete row) background colour change to Orange.

Any help with this would be great

Kris


回答1:


If you are going to change the DataGridCell.Background property you need to use it somewhere in your ControlTemplate

e.g. using a TemplateBinding

<Border Name="DataGridCellBorder"
        Background="{TemplateBinding Background}"/>


来源:https://stackoverflow.com/questions/9382029/datagrid-selected-cell-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!