天天看點

WPF XAML 資源樣式模闆屬性存放位置

原文: WPF XAML 資源樣式模闆屬性存放位置

WPF的XAML 資源申明 類似HTML。

整體來說分3種

1.行類資源樣式屬性

1.1 行内屬性

<Button Content="按鈕" Foreground="White" FontSize="30"></Button>
      

1.2 行内樣式 模闆(沒多大意義)

<Button Content="按鈕">
  <Button.Style>
         <Style TargetType="Button">
                    <Setter Property="Foreground" Value="White"></Setter>
                    <Setter Property="FontSize" Value="30"></Setter>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="Button">
                                <Grid></Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
          </Style>
  </Button.Style>
  <Button.Template>
           <ControlTemplate TargetType="Button">
                    <Grid></Grid>
           </ControlTemplate>
  </Button.Template>
</Button>      

2.頭部資源樣式屬性

2.1 頭部樣式模闆 (申明)

<Window.Resources>
        <Style x:Key="xxx" TargetType="Button">
            <Setter Property="Foreground" Value="White"></Setter>
            <Setter Property="FontSize" Value="30"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid></Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <ControlTemplate x:Key="xx" TargetType="Button">
            <Grid></Grid>
        </ControlTemplate>
</Window.Resources>
      

2.2 頭部樣式模闆 (引用)

<Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
</Window.Resources>
      

  

3.全局資源樣式屬性

App.xaml

<Application.Resources>
        <ResourceDictionary>

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <Style x:Key="xxx" TargetType="Button">
                <Setter Property="Foreground" Value="White"></Setter>
                <Setter Property="FontSize" Value="30"></Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Grid></Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <ControlTemplate x:Key="xx" TargetType="Button">
                <Grid></Grid>
            </ControlTemplate>
        </ResourceDictionary>
    </Application.Resources>
      

!!!!!!!!!!!!!!配合資源字典的使用!!!!!!!!!  

說明:

1.行類屬性盡量少用,隻有特殊控件 需要用到行内屬性,

   正确的做法是封裝統一風格的所有控件。

  (例如按鈕,統一高寬,字型,字型大小,然後申明到獨立的資源字典中,

    在App.xaml中引用)

2.頭部資源引用情況用于 不同 Window 适應不同主題或者風格的情況。

   比如為某一個視窗申明一個目前視窗單獨使用的樣式。

  (例如播放器的旋轉控件,隻有一個頁面用到,隻需要在Window級引用對應資源字典)

   不放在App.xaml原因是為了降低記憶體消耗。

3.App.xaml 裡面的資源引用适用于全局資源。理論上每一個被申明的Window

   都會建立一個對應資源字典的執行個體。除非是每個Window都會用到的子產品,

    不然建議放到對應Window級