简体   繁体   English

如何将DataGrid.Resources应用于所有DataGrid

[英]How To Apply DataGrid.Resources To All DataGrids

I have created a WPF project with multiple DataGrid controls. 我创建了带有多个DataGrid控件的WPF项目。 I want them to display selected rows in the normal style even when the DataGrid loses focus. 我希望他们以正常样式显示选定的行,即使DataGrid失去焦点。 The code below works, but I want an easy way to apply this to all the DataGrids in the project without manually inserting this code into every DataGrid in the project. 下面的代码有效,但是我希望有一种简单的方法将其应用于项目中的所有DataGrid,而无需手动将此代码插入项目中的每个DataGrid。 Can this be done in the app.xaml file or some other central resource? 可以在app.xaml文件或其他中央资源中完成此操作吗?

  <DataGrid Name="dgStores" AutoGenerateColumns="True" AutoGeneratedColumns="DataGrid_AutoGeneratedColumns" > <DataGrid.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DodgerBlue"/> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DodgerBlue"/> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/> </DataGrid.Resources> </DataGrid> 

Create a style that targets DataGrid and add your resources to the style's resources and place this above the DataGrids in the visual tree (eg App.xaml): 创建一个以DataGrid为目标的样式,并将您的资源添加到该样式的资源中,并将其放置在可视树中的DataGrid上方(例如App.xaml):

<Style TargetType="DataGrid">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="DodgerBlue"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="DodgerBlue"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White"/>
    </Style.Resources>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM