【问题标题】:Prevent Rectangle Style From Applying to DataGrid防止矩形样式应用于 DataGrid
【发布时间】:2016-06-01 07:16:50
【问题描述】:

我在我的app.config 中设置了一个style,我想为我在程序中使用的每个rectangle 着色。好看又简单,长这样;

<Style TargetType="Rectangle">
    <Setter Property="Fill" Value="LightBlue"/>
</Style>

我最近注意到我的所有DataGrids 中有一些奇怪的行为,当用户使用箭头键导航到rows 时,单元格内的文本将被空白,看起来像这样;

之前

按下箭头键后

如您所见,文本已明显消失(两张图片中的单元格相同)。我终于开始研究这个并得出结论,rectangle 样式适用于每个DataGridCell。我只能假设这是因为wpfDataGridCell 中使用了recangles

有没有办法可以将此样式仅应用于我从工具箱中取出的rectangles,而不是DataGrid 中的文字rectangles

一个简单的选择是完全删除 style 并单独应用它,但如果我们进一步更改配色方案,这可能会变得非常耗时。

更新

我还(如果可能的话)喜欢使用任何修复程序来修改我喜欢的DataGridCell 外观;

<Style TargetType="DataGridCell">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Orange" />
            <Setter Property="BorderBrush" Value="Black" />
        </Trigger>
    </Style.Triggers>
</Style>

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    DataGridCells 使用 FocusVisualStyle,它的可视化树中有 Rectangle。这些矩形从默认的 Rectangle 样式中获得 Fill 画笔。如果您更改FocusVisualStyle,则单元格将在键盘选择中可见

    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        </Style>
    </DataGrid.CellStyle>
    

    如果DataGridCell 已经有默认样式,请在此处添加一个设置器

    <Style TargetType="DataGridCell">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Orange" />
                <Setter Property="BorderBrush" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>
    


    附加信息:我对Buttons 和使用Tab 的选择有同样的问题

    【讨论】:

    • 嗨灰,我已经更新了我的问题。您的修复确实可以保留文本,但是它(如我所料)删除了我想应用于DataGridCell 的样式。有没有办法两者兼得?
    • 抱歉提出了一个旧的(ish)线程。但现在当我做诸如dataGrid.Focus()之类的事情时,整个DataGrid都被填充了LightBlue。我再次假设在DataGrid 中使用了矩形,有没有办法防止这种情况发生?同样,正如您在回答中所说,我在通过Buttons 切换时也遇到了这个问题。您是如何解决Button 问题的?
    • @CBreeze,看起来又是他们的FocusVisualStyle,基本上是一个矩形,但没有默认填充值。这篇文章可能会有所帮助:stackoverflow.com/questions/1055670/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多