引用usercontrol中的资源不起作用

时间:2018-01-17 20:47:51

标签: wpf xaml

我有一个具有以下结构的Wpf应用程序

MainSolution - >视图(文件夹) - > NextGenDG(WPF项目) - > DisplayResources(文件夹) - > BRUserControlStyles.xaml

这是BRUserControlStyles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:NextGenDG.DisplayResources">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary
            Source="/NextGenDG;component/DisplayResources/BRColors.xaml" />
        <ResourceDictionary
            Source="/NextGenDG;component/DisplayResources/BRFonts.xaml" />
        <ResourceDictionary
            Source="/NextGenDG;component/DisplayResources/BRButtonStyles.xaml" />
        <ResourceDictionary
            Source="/NextGenDG;component/DisplayResources/BRCommonControlStyles.xaml" />
        <ResourceDictionary
            Source="/NextGenDG;component/DisplayResources/BRToggleButtonStyles.xaml" />
    </ResourceDictionary.MergedDictionaries>
    <Style
        x:Key="UserControlDarkBackgroundStyle"
        TargetType="{x:Type UserControl}"
        BasedOn="{StaticResource {x:Type UserControl}}">
        <Setter
            Property="Margin"
            Value="0" />
        <Setter
            Property="Padding"
            Value="0" />
        <Setter
            Property="Background"
            Value="Red" />
        <Style.Resources>
            <Style
                TargetType="{x:Type Button}"
                BasedOn="{StaticResource ButtonLightTextGradientDarkBackgroundStyle}" />
            <Style
                TargetType="{x:Type TextBlock}"
                BasedOn="{StaticResource TextBlockLightTextStyle}" />
            <Style
                TargetType="{x:Type ToggleButton}"
                BasedOn="{StaticResource ToggleButtonLightTextGradientDarkBackgroundStyle}" />
            <Style
                TargetType="{x:Type Separator}"
                BasedOn="{StaticResource SeparatorDarkLineStyle}" />
        </Style.Resources>
    </Style>
</ResourceDictionary>

现在我正在尝试使用UserControl内部的样式,该样式位于名为UserControls的文件夹中,如下所示。在MainWindow中调用此UserControl

<UserControl x:Class="NextGenDG.UserControls.NextGenHeaderCartridgeStatusUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:NextGenDG.UserControls"
             mc:Ignorable="d"
            Style="{DynamicResource UserControlDarkBackgroundStyle}"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
<TextBlock Grid.Column="1"
            Text="{Binding Text}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
</UserControl>

问题是,如果我说StaticResource UserControlDarkBackgroundStyle它抱怨说没有找到资源。因此,当我使用DynamicResource UserControlDarkBackgroundStyle时,它不会应用该样式。请帮忙。在glace是我的ResourceDictionary路径正确吗?

0 个答案:

没有答案