2016-07-08 16 views
0

我正在创建一个简单的WPF Gui。我试图将隐式样式应用于所有UserControl,以使它们具有一些默认外观 - 背景颜色和字体。这似乎对用户控制本身起作用。但是,当用户控件嵌入到窗口中时,样式是列表。嵌入窗口时不应用UserControl样式?

例如,下面是一个用户控件:

UserControl

代码:

<UserControl x:Class="NtpClient.Gui.Components.MainPanel.MainPanelView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:serverSelector="clr-namespace:NtpClient.Gui.Components.ServerSelector" 
     xmlns:detailsPanel="clr-namespace:NtpClient.Gui.Components.DetailsPanel" 

     Height="350" Width="400"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <serverSelector:ServerSelectorView Grid.Column="0" Padding="6" /> 
     <detailsPanel:DetailsPanelView Grid.Column="1" Padding="6" /> 
    </Grid> 
</UserControl> 

这里是在窗口与用户控件:

Window

代码:

<Window x:Class="NtpClient.Gui.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mainPanel="clr-namespace:NtpClient.Gui.Components.MainPanel" 
     Title="Network Time Servers" Height="350" Width="400"> 
    <mainPanel:MainPanelView Width="auto" Height="auto"/> 
</Window> 

的主题文件被设置在app.xaml,看起来像这样:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="/Themes_CoreThemes;component/ControlStyles.xaml" /> 
     <ResourceDictionary Source="/Themes_CoreThemes;component/BrushResources.xaml" /> 
    </ResourceDictionary.MergedDictionaries>  

    <!-- Implicit Styles --> 
    <Style BasedOn="{StaticResource DeepBlueButtonStyle}" TargetType="Button" /> 
    <Style BasedOn="{StaticResource ControlStyle}" TargetType="UserControl" /> 
</ResourceDictionary> 

的App.xaml:

<Application x:Class="NtpClient.Gui.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="/Themes_CoreThemes;component/ControlStyles.xaml" /> 
       <ResourceDictionary Source="/Themes_CoreThemes;component/BrushResources.xaml" /> 
       <ResourceDictionary Source="Themes/Local/Local.MSControls.Core.Implicit.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 
</Application> 

有没有人有任何想法是什么原因造成这一点,我怎么解决它?

+0

你能发布App.xaml代码吗? –

+0

@HenkaProgrammer已经这么做了。 – Oliver

+1

看看这篇文章:http://stackoverflow.com/questions/7470621/issue-with-applying-style-on-wpf-usercontrol –

回答

-1

我在几个月前面临这个问题。
这是因为你的xaml文件在其他dll中。
我不知道为什么,但设计师没有正确处理。
看起来是设计师的外在问题。
我刚刚将样式从dll移至主项目。