WPF Merged ResourceDictionary 与绑定到另一个字典中的资源的资源不起作用

WPF Merged ResourceDictionary with resource that binds to resource in another Dictionary not working

我创建了一个简单的项目来演示我遇到的错误所在: “在 'System.Windows.Markup.StaticResourceHolder' 上提供值引发异常。”行号“6”和行位置“9”。

项目布局很简单,我已经上传到dropbox: https://www.dropbox.com/s/451b5zkw8oqgcld/StyleTest1.zip?dl=0

MainWindow.xaml

<Window x:Class="StyleTest1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
                <ResourceDictionary Source="Dictionary2.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
                <Setter Property="Background" Value="{DynamicResource Button.Static.Background}"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Style="{DynamicResource ButtonStyle1}">

        </Button>
    </Grid>
</Window>

Dictionary1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <GradientStopCollection po:Freeze="true" x:Key="ButtonBackgroundStops">
        <GradientStop Color="#2d2d2f"/>
        <GradientStop Color="#2d2d2f" Offset="1"/>
    </GradientStopCollection>

    <LinearGradientBrush
        po:Freeze="true"    
        x:Key="ButtonBackgroundBrush"           
        GradientStops="{StaticResource ButtonBackgroundStops}"  
        StartPoint="0.5,-0.05"  
        EndPoint="0.5,0.66" />

</ResourceDictionary>

Dictionary2.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <LinearGradientBrush 
        x:Key="Button.Static.Background" 
        GradientStops="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=GradientStops}" 
        StartPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=StartPoint}" 
        EndPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=EndPoint}"/>

</ResourceDictionary>

就是这样...如果我 运行 该程序我得到错误: “在 'System.Windows.Markup.StaticResourceHolder' 上提供值引发异常。”行号“6”和行位置“9”。

但是,如果我将 MainWindow.xaml 更改为以下内容,我将不再遇到此问题: 这是修改后版本的保管箱 link: https://www.dropbox.com/s/ceikh5b8cfecdkw/StyleTest2.zip?dl=0

MainWindow.xaml

<Window x:Class="StyleTest2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:StyleTest2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary1.xaml" />
                <ResourceDictionary Source="Dictionary2.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <LinearGradientBrush 
                x:Key="Button.Static.Background" 
                GradientStops="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=GradientStops}" 
                StartPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=StartPoint}" />
            <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
                <Setter Property="Background" Value="{DynamicResource Button.Static.Background}"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Style="{DynamicResource ButtonStyle1}">

        </Button>
    </Grid>
</Window>

这表明 Dictionary2.xaml 中的 LinearGradientBrush 与位于 Dictionary1.xaml 中的 ButtonBackgroundBrush 资源绑定存在问题。

谁能告诉我我做错了什么以及让一个词典中的资源引用另一个词典中的资源的正确方法是什么?

感谢您的宝贵时间,

codeOwl

  1. 在 Dictionary2 中使用 DynamicResource 代替 StaticResource

  2. 在Dictionary2中合并Dictionary 1,就没有问题了。

Dictionary2 看起来像:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Dictionary1.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <LinearGradientBrush 
        x:Key="Button.Static.Background" 
        GradientStops="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=GradientStops}" 
        StartPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=StartPoint}" 
        EndPoint="{Binding Source={StaticResource ButtonBackgroundBrush}, Path=EndPoint}"/>

</ResourceDictionary>