WPF 数据网格样式选定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15541480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:12:42  来源:igfitidea点击:

WPF Datagrid Style Selected Row

wpfxamlvisual-studio-2012wpfdatagridexpression-blend

提问by J King

I have a wpf 4.5 application that has a datagrid in a usercontrol. I designed the style for the datagrid in Blend but when I copy the style into my VS 2012 project it is not working properly.

我有一个 wpf 4.5 应用程序,它在用户控件中有一个数据网格。我在 Blend 中为数据网格设计了样式,但是当我将样式复制到我的 VS 2012 项目中时,它无法正常工作。

Here is what the base style looks like in Blend:

下面是 Blend 中的基本样式:

Blend DataGrid Style

混合数据网格样式

Please note the color of the text in the selected row (green) and the padding around the textbox that is being edited

请注意所选行中文本的颜色(绿色)以及正在编辑的文本框周围的填充

Now here is what the same style looks like Visual Studio 2012:

现在这里是与 Visual Studio 2012 相同的样式:

VS Datagrid Style

VS 数据网格样式

So the highlighted text from the selected row is the same color as the background (making it invisible), and I can't change the textbox bordercolor or margin to match the blend style.

因此,所选行中突出显示的文本与背景颜色相同(使其不可见),并且我无法更改文本框边框颜色或边距以匹配混合样式。

here is the cell style:

这是单元格样式:

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border x:Name="border"
                        BorderBrush="Transparent"
                        BorderThickness="1"
                        Background="Transparent"
                        SnapsToDevicePixels="True"
                        Margin="15,10,15,10">
                    <ContentPresenter Name="CellContent" SnapsToDevicePixels="True" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Foreground" Value="Green"/>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="BorderBrush" TargetName="border" Value="#FFD8D8D8"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>                
        </Setter.Value>
    </Setter>
</Style>

so you can see that the trigger marking green as the foregorund color works in blend but not visual studio. I think it is a system color that is overriding the trigger some how but I am not sure how to track this down.

因此您可以看到将绿色标记为前景色的触发器在混合中起作用,但在视觉工作室中不起作用。我认为这是一种系统颜色,它以某种方式覆盖了触发器,但我不确定如何追踪它。

Can someone please help me correct this style problem by setting the selected row text color and textbox border/margins.

有人可以通过设置选定的行文本颜色和文本框边框/边距来帮助我纠正此样式问题。

This is a link to the entire datagrid style resource dictionary

这是整个数据网格样式资源字典的链接

thanks in advance

提前致谢

采纳答案by J King

So it turned out to be a conflict of the DataGridCell style. I had a DataGridCell style applied without key name to all datagrid cells. Then I created another cell style based on the one mentioned above But for some reason if I tried to apply this cell style to specific column declarations in the datagrid the style would break. So I had to remove the "based on styles" and find another way to do column specific formatting.

所以原来是DataGridCell风格的冲突。我对所有数据网格单元格应用了一个没有键名的 DataGridCell 样式。然后我基于上面提到的那个创建了另一种单元格样式但是由于某种原因,如果我尝试将此单元格样式应用于数据网格中的特定列声明,样式就会中断。所以我不得不删除“基于样式”并找到另一种方法来进行特定于列的格式化。

EDIT: see style here, too long to post DataGridStyle

编辑:在此处查看样式,太长无法发布DataGridStyle

回答by makim

Hi I tried your Style and on my Machine the ForegroundColor changed to Green... But if it doesn′t work in your DevEnvironment you could try overriding the SystemColor for the selected-row text!

嗨,我尝试了你的风格,在我的机器上,前景颜色变成了绿色......但是如果它在你的开发环境中不起作用,你可以尝试覆盖所选行文本的系统颜色!

add this to your style and remove the trigger

将此添加到您的样式并删除触发器

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Green" />