全局資源樣式屬性
App.xaml
<Application.Resources> <ResourceDictionary><br> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Dictionary.xaml"/> </ResourceDictionary.MergedDictionaries><br> <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>說(shuō)明:
1.行類(lèi)屬性盡量少用,只有特殊控件 需要用到行內屬性,
正確的做法是封裝統一風(fēng)格的所有控件。
(例如按鈕,統一高寬,字體,字體大小,然后申明到獨立的資源字典中,
在A(yíng)pp.xaml中引用)
2.頭部資源引用情況用于 不同 Window 適應不同主題或者風(fēng)格的情況。
比如為某一個(gè)窗口申明一個(gè)當前窗口單獨使用的樣式。
(例如播放器的旋轉控件,只有一個(gè)頁(yè)面用到,只需要在Window級引用對應資源字典)
不放在A(yíng)pp.xaml原因是為了降低內存消耗。
3.App.xaml 里面的資源引用適用于全局資源。理論上每一個(gè)被申明的Window
都會(huì )創(chuàng )建一個(gè)對應資源字典的實(shí)例。除非是每個(gè)Window都會(huì )用到的模塊,
不然建議放到對應Window級
經(jīng)典實(shí)例:
ControlStyle.xaml<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DemoForm.UI"> <Style x:Key="BtnControl" TargetType="Button"> <Setter Property="FontSize" Value="15"/> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Height" Value="40"/> <Setter Property="Margin" Value="2"/> <!--<Setter Property="Background" Value="Red"/>--></Style></ResourceDictionary>
App.xaml<Application x:Class="DemoForm.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:DemoForm" StartupUri="MainWindow.xaml">
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/UI/ControlStyle.xaml"></ResourceDictionary> <!--或者這樣方式DemoForm;component/UI/Dictionary1.xaml 引用以后就可以繼承了--> </ResourceDictionary.MergedDictionaries> <Style BasedOn="{StaticResource BtnControl}" TargetType="Button" > <Setter Property="FontSize" Value="10" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="Height" Value="40" /> <Setter Property="Margin" Value="2" /> <Setter Property="Template"> <!--應用于全局的控件模板--> <Setter.Value> <ControlTemplate TargetType="Button"> <Border BorderThickness="1" CornerRadius="10" Background="{TemplateBinding Background}"> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> </Border> <ControlTemplate.Triggers > <Trigger Property="Button.IsMouseOver" Value="True"> <Setter Property="Button.Background" Value="blue"/> </Trigger > </ControlTemplate.Triggers > </ControlTemplate> </Setter.Value> </Setter></Style> <Style TargetType="Label"> <!--//x:Key="LblStyle"去掉就是全局引用--> <Setter Property="FontSize" Value="12" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /></Style> <Style TargetType="TextBox"> <!--//x:Key="TxtStyle" 去掉就是全局引用--> <Setter Property="FontSize" Value="12" /> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="MaxHeight" Value="50" /> <Setter Property="MinWidth" Value="80" /> <Setter Property="Margin" Value="2" /></Style> <!--<ControlTemplate x:Key="buttonTemplate" TargetType="Button" > <Border BorderThickness="1" CornerRadius="10" Background="{TemplateBinding Background}"> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/> </Border> <ControlTemplate.Triggers > <Trigger Property="Button.IsMouseOver" Value="True"> <Setter Property="Button.Background" Value="blue"/> </Trigger > </ControlTemplate.Triggers > </ControlTemplate >--> </ResourceDictionary> </Application.Resources></Application>
聯(lián)系客服