【问题标题】:WPF UserControl: Use default WPF style rather than inheriting style of parent applicationWPF UserControl:使用默认 WPF 样式而不是继承父应用程序的样式
【发布时间】:2015-06-07 01:42:02
【问题描述】:

我在应用程序中嵌入了一个 WPF 用户控件,它继承了应用程序的样式。我在设计用户控件时考虑了默认的 WPF 样式,它没有定义自己的资源/样式。但是,由于继承了宿主应用程序的样式,它看起来并不正确。如果用户控件只使用默认的 WPF 样式,我会更喜欢。有没有一种简单的方法可以做到这一点?

UserControl 本身包含一个System.Windows.Controls.Frame,它引用了一个System.Windows.Control.Page

<UserControl x:Class="OrderParcelAddInPrototype.WpfControls.Controls.OrderParcelControl"
             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" 
             mc:Ignorable="d"  d:DesignHeight="300" d:DesignWidth="450">
    <Frame Source="/OrderParcelAddInPrototype.WpfControls;component/Pages/MainPage.xaml" />
</UserControl>

UserControlPage 都没有定义自己的样式。

<Page x:Class="OrderParcelAddInPrototype.WpfControls.Pages.StartPage"
        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"
        mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="450" Title="Main Page">

    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <StackPanel VerticalAlignment="Top" MaxWidth="350" CanVerticallyScroll="True"
                    ScrollViewer.CanContentScroll="True">
            <!-- Removed for brevity -->
        </StackPanel>
    </ScrollViewer>
</Page>

【问题讨论】:

  • 请发布您的代码/XAML ?
  • 你试过&lt;UserControl Style="{StaticResource {x:Type UserControl}}" /&gt;吗?
  • 我刚刚试了一下。它似乎无法解决问题。

标签: wpf xaml wpf-controls wpf-style


【解决方案1】:

在用户控件资源中定义您的用户控件样式并将该样式应用到用户控件中

 <UserControl x:Class="MyNamespace.MyUserControl"
         ...
         Style="{DynamicResource ResourceKey=MyUserControlStyle}">
<UserControl.Resources>
    ...
    <Style x:Key="MyUserControlStyle" TargetType="{x:Type UserControl}">

    </Style>
</UserControl.Resources>

否则,您可以使用合并用户控件的资源字典 app.xaml 中资源字典中的mergeddictionary

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多