Wpf 如何让我的用户控件使用与App.xaml相同的样式?

Wpf 如何让我的用户控件使用与App.xaml相同的样式?,wpf,styles,Wpf,Styles,我有一个UserControl,其中有一个Border元素,我想用一个特定的Border样式来设置它的样式。它会编译但不会启动,给出一个XamlParseException,说“找不到资源…” 有办法做到这一点吗 谢谢 App.xaml: <cal:CaliburnApplication x:Class="WahnamProgressTracker.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

我有一个UserControl,其中有一个Border元素,我想用一个特定的Border样式来设置它的样式。它会编译但不会启动,给出一个XamlParseException,说“找不到资源…”

有办法做到这一点吗

谢谢

App.xaml:

<cal:CaliburnApplication x:Class="WahnamProgressTracker.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:Converters="clr-namespace:WahnamProgressTracker.Converters;assembly=WahnamProgressTracker"
xmlns:Model="clr-namespace:WahnamProgressTracker.Model">
<Application.Resources>
    <Style x:Key="FancyBorder"
           TargetType="{x:Type Border}">
        <Setter Property="Margin" Value="0,0,0,8"/>
        <Setter Property="Padding" Value="8"/>
        ...
    </Style>
</Application.Resources>
<Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Window1.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type TextBlock}" x:Key="myStyle">
            <Setter Property="Foreground" Value="Green" />
            <Setter Property="FontWeight" Value="Bold" />
        </Style>
    </Application.Resources>
</Application>

...

MainView.xaml:

<Window x:Class="WahnamProgressTracker.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:uc="clr-namespace:WahnamProgressTracker.UserControls"
MinHeight="500" MinWidth="800">

<DockPanel>
    <uc:MainViewMenu x:Name="menu"
                     DockPanel.Dock="Top" />

    <StatusBar x:Name="quoteBar"                   
               DockPanel.Dock="Bottom">
        <TextBlock Text="{Binding Path=Quote.Text, Mode=OneWay}" />
    </StatusBar>

    <uc:MainViewNavigation x:Name="navigationBar"
                           DockPanel.Dock="Left" />

    <uc:ProgressGraph x:Name="graph" />
</DockPanel>

MainViewNavigation.xaml(用户控件):


...       

你能发布一个你的意思的示例吗?出现问题的唯一情况是创建用户控件,然后在应用程序的可视树之外呈现

下面的XAML适用于我:

App.xaml:

<cal:CaliburnApplication x:Class="WahnamProgressTracker.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:Converters="clr-namespace:WahnamProgressTracker.Converters;assembly=WahnamProgressTracker"
xmlns:Model="clr-namespace:WahnamProgressTracker.Model">
<Application.Resources>
    <Style x:Key="FancyBorder"
           TargetType="{x:Type Border}">
        <Setter Property="Margin" Value="0,0,0,8"/>
        <Setter Property="Padding" Value="8"/>
        ...
    </Style>
</Application.Resources>
<Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Window1.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type TextBlock}" x:Key="myStyle">
            <Setter Property="Foreground" Value="Green" />
            <Setter Property="FontWeight" Value="Bold" />
        </Style>
    </Application.Resources>
</Application>

Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <local:UserControl1 />
    </Grid>
</Window>

UserControl1.xaml:

<UserControl x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <TextBlock Style="{StaticResource myStyle}">HEY!</TextBlock>
    </Grid>
</UserControl>

hmmm。。。我的用户控件是否位于不同的命名空间中?它应该是“x:Class=“WpfApplication1.UserControls.UserControl1”…不,这不重要。如果可能的话,我建议您在问题中添加一些代码。这样诊断会更容易。出于某种原因,我发布的前两个代码段的最后一行也是如此,但请放心,它们都在代码中。嘿,siz,你忘了这个问题了吗?我还没有弄明白(一直在努力解决)