【问题标题】:How do I let my UserControls use the same styles as App.xaml?如何让我的 UserControls 使用与 App.xaml 相同的样式?
【发布时间】:2009-08-27 00:08:21
【问题描述】:

我有一个 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>

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(用户控件):

<UserControl x:Class="WahnamProgressTracker.UserControls.MainViewNavigation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Border Style="{StaticResource FancyBorder}">
        ...       
    </Border>    
</UserControl>

【问题讨论】:

    标签: wpf styles


    【解决方案1】:

    您能否发布一个示例来说明您的意思?唯一可能出现问题的情况是创建了用户控件,然后在应用程序的可视化树之外呈现。

    下面的 XAML 对我有用:

    App.xaml:

    <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""...
    • 不,没关系。如果可能,我建议您在问题中添加一些代码。这样会更容易诊断。
    • 出于某种原因,SO 正在删除我发布的前两个代码段的最后一行,但请放心,它们在代码中
    • 嘿,你忘了这个问题了吗?我还没有弄清楚(一直在解决它)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多