2016-07-08 15 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移至主項目。