繁体   English   中英

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

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

我创建了带有多个DataGrid控件的WPF项目。 我希望他们以正常样式显示选定的行,即使DataGrid失去焦点。 下面的代码有效,但是我希望有一种简单的方法将其应用于项目中的所有DataGrid,而无需手动将此代码插入项目中的每个DataGrid。 可以在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> 

创建一个以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