簡體   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