DataGrid选定的单元格背景 [英] DataGrid Selected Cell Background

查看:58
本文介绍了DataGrid选定的单元格背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用WPF DataGrid时遇到问题.

我有以下代码.

 < 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}"><边界名称="DataGridCellBorder">< ContentControl Content ="{TemplateBinding Content}">< ContentControl.ContentTemplate>< DataTemplate>< TextBlock宽度=自动"高度=自动"背景=透明"文字="{绑定文字}"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></样式> 

如您所见,我已经用自定义模板替换了默认的DataGridCell.这是需要的,因为如果文本太大而无法容纳到给定的单元格中,我希望这些单元格具有TextTrimming(当用户将鼠标悬停在给定的单元格上时,此提示会在工具提示中完全显示)

我现在的问题是,当我选择一个单元格时,无论我尝试做什么,前景都会设置为白色"-我真正想发生的事情是更改单元格(甚至更好的是整行)的背景颜色到橘子.

任何对此的帮助都将很好

克里斯

解决方案

如果要更改 DataGridCell.Background 属性,则需要在 ControlTemplate

例如使用 TemplateBinding

 <边框名称="DataGridCellBorder"Background ="{TemplateBinding Background}"/> 

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

解决方案

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}"/>

这篇关于DataGrid选定的单元格背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆