2016-04-08 63 views

回答

0

您可以的了ResourceDictionary中定義樣式家長控制。在Window.Resources中定義的樣式適用於所有的矩形,因爲它沒有指定一個鍵,所以第一個StackPanel中的矩形是黃色和小的。第二個StackPanel定義了它自己的資源,它的子代使用它們,並且它們結束了不同的顏色和更大的尺寸。還有風格的傳承中如果你想在不同的表樣式有使用支持算法FMP

<Window x:Class="GBTest.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-GBTest" 
    mc:Ignorable="d" 
    Title="MainWindow" 
    Height="350" 
    Width="525"> 
<!--Default style defined in the Window Resources--> 
<Window.Resources> 
    <Style TargetType="Rectangle"> 
     <Setter Property="Width" 
       Value="100" /> 
     <Setter Property="Height" Value="100" /> 
     <Setter Property="Fill" 
       Value="Yellow" /> 
    </Style> 
</Window.Resources> 
<StackPanel> 
    <Rectangle /> 
    <StackPanel> 
     <!--The styles defined in the resources of this StckPanel will apply to its children, overriding the default style defined in the Window Resources--> 
     <StackPanel.Resources> 
      <Style TargetType="Rectangle" 
        x:Key="BigStyle"> 
       <Setter Property="Height" 
         Value="200" /> 
       <Setter Property="Width" 
         Value="200" /> 
      </Style> 
      <Style TargetType="Rectangle" 
        x:Key="RedStyle" 
        BasedOn="{StaticResource BigStyle}"> 
       <Setter Property="Fill" 
         Value="Red" /> 
      </Style> 
      <Style TargetType="Rectangle" 
        BasedOn="{StaticResource BigStyle}" 
        x:Key="BlueStyle"> 
       <Setter Property="Fill" 
         Value="Blue" /> 
      </Style> 
     </StackPanel.Resources> 

     <Rectangle Style="{StaticResource RedStyle}" /> 
     <Rectangle Style="{StaticResource BlueStyle}" /> 
    </StackPanel> 
</StackPanel> 

+0

感謝您的回放,雖然這可以完成這項工作,但這仍然意味着我在控件本身中定義了樣式。我想要的是完全分開的樣式和控件。有點像CSS,我在一個單獨的文件中定義了一切。這就是爲什麼我認爲Style.Resources很高興有 – NicolasL

1

(你應該。我發現在控制自己,因爲我誤解了樣式,並認爲這是你想要的東西)您可以創建一個資源文件夾並添加不同的資源詞典。就個人而言,我通常爲畫筆,樣式,模板和轉換器創建單獨的字典。然後你聲明它們在ResourceDictionary.MergedDictionaries在App.xaml中

<Application 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
RequestedTheme="Light"> 
<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="/Resources/Brushes.xaml" /> 
      <ResourceDictionary Source="/Resources/Styles.xaml" /> 
      <ResourceDictionary Source="/Resources/Converters.xaml" /> 
      <ResourceDictionary Source="/Resources/Templates.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Application.Resources> 

+0

對不清楚我是xaml的新手,我明白這一點。讓我們把你的Styles.xaml放在MergedDictionaries中,我將如何定義自定義控件的樣式和它的子控件。把你之前的例子與StackPanel結合起來,我想你會有一個帶StackPanel的Style作爲TargetType,然後將StackPanel的Style設置爲StaticResource,但是在它裏面定義的矩形呢?有了Style.Resources,我似乎可以在Styles.xaml中獲得一個自定義控件的樣式層次結構,但UWP中沒有這個樣式,這就是我想要的。謝謝你的時間。 – NicolasL

相關問題