XAML WinUI3 DataGrid如何在应用程序资源中而不是在数据网格中设置行颜色

bvuwiixz  于 2023-08-01  发布在  其他
关注(0)|答案(1)|浏览(121)

我想将DataGrid资源的颜色覆盖移动到应用程序资源,这样就不必为应用程序中的每个DataGrid设置颜色覆盖。当我将颜色从DataGrid资源移动到Application资源时,颜色不会被应用。我做错了什么?
Resources defined in DataGrid
Resource defined in Application
在资源中这样设置行颜色不会更改数据网格的行颜色

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                <ResourceDictionary>
                    <ui:Color x:Key="DataGridRowHoveredBackgroundColor">Green</ui:Color>
                    <ui:Color x:Key="DataGridRowSelectedHoveredBackgroundColor">Green</ui:Color>
                    <ui:Color x:Key="DataGridRowSelectedHoveredUnfocusedBackgroundColor">Green</ui:Color>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

字符串
像这样在数据网格资源中设置行颜色确实有效

<controls:DataGrid.Resources>
    <ui:Color x:Key="DataGridRowHoveredBackgroundColor">Green</ui:Color>
    <ui:Color x:Key="DataGridRowSelectedHoveredBackgroundColor">Green</ui:Color>
    <ui:Color x:Key="DataGridRowSelectedHoveredUnfocusedBackgroundColor">Green</ui:Color>           
</controls:DataGrid.Resources>
xmlns:ui="using:Windows.UI"
xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
lyfkaqu1

lyfkaqu11#

在我们的应用程序资源中定义的资源不能覆盖DataGrid.xaml中的资源。有关更多信息,请参见XAML资源引用的查找行为。
但每个FrameworkElement都可以有一个ResourceDictionary。您可以在父元素的字典中定义这些资源。我测试了它的工作。

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel.Resources>
            <Color x:Key="DataGridRowHoveredBackgroundColor">Green</Color>
            <Color x:Key="DataGridRowSelectedHoveredBackgroundColor">Green</Color>
            <Color x:Key="DataGridRowSelectedHoveredUnfocusedBackgroundColor">Green</Color>
        </StackPanel.Resources>
        
        <YANG:DataGrid x:Name="dataGrid1" 
    Height="600" Margin="12"
    AutoGenerateColumns="True"
    ItemsSource="{x:Bind Customers}" >
            <!--<YANG:DataGrid.Resources>
                <Color x:Key="DataGridRowHoveredBackgroundColor">Green</Color>
                <Color x:Key="DataGridRowSelectedHoveredBackgroundColor">Green</Color>
                <Color x:Key="DataGridRowSelectedHoveredUnfocusedBackgroundColor">Green</Color>
            </YANG:DataGrid.Resources>-->
        </YANG:DataGrid>
    </StackPanel>

字符串
另见类似线程https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/3946
更新:
奇怪的是,它不适用于DataGrid,但适用于其他控件,如ComboBox和Buttons。
正如我们在生成的App.xaml中看到的,Microsoft.UI.Xaml.Controls资源被合并。
根据合并字典行为:
合并字典中的资源在资源查找范围中占据一个位置,该位置正好在它们合并到的主资源字典的范围之后。尽管资源键在任何单独的字典中必须是唯一的,但是一个键可以在一组合并的字典中存在多次。在这种情况下,返回的资源将来自MergedDictionaries集合中按顺序找到的最后一个字典。如果MergedDictionaries集合是在XAML中定义的,则集合中合并字典的顺序是标记中提供的元素的顺序。如果在主字典中定义了一个键,并且在合并的字典中也定义了一个键,那么返回的资源将来自主字典。这些作用域规则同样适用于静态资源引用和动态资源引用。

<!-- Copyright (c) Microsoft Corporation and Contributors. -->
<!-- Licensed under the MIT License. -->

<Application
    x:Class="App2CSharp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App2CSharp">
    <Application.Resources>
        <ResourceDictionary>
            <!--<Color x:Key="ButtonBackgroundPointerOver">Green</Color> work-->
            <!--<SolidColorBrush x:Key="MyBrush" Color="Green"></SolidColorBrush>
            <StaticResource x:Key="ButtonBackgroundPointerOver" ResourceKey="MyBrush"/> work-->

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <Color x:Key="ButtonBackgroundPointerOver">Green</Color><!--doesn't work-->
                </ResourceDictionary>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                <!-- Other merged dictionaries here -->
                <!--<SolidColorBrush x:Key="MyBrush" Color="Green"></SolidColorBrush>
                <StaticResource x:Key="ButtonBackgroundPointerOver" ResourceKey="MyBrush"/> work-->
            </ResourceDictionary.MergedDictionaries>
            <!-- Other app resources here -->
        </ResourceDictionary>
    </Application.Resources>
</Application>

相关问题