全局資源樣式屬性 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> 說明: 1.行類屬性盡量少用,只有特殊控件 需要用到行內(nèi)屬性, 正確的做法是封裝統(tǒng)一風(fēng)格的所有控件。 2.頭部資源引用情況用于 不同 Window 適應(yīng)不同主題或者風(fēng)格的情況。 比如為某一個窗口申明一個當(dāng)前窗口單獨(dú)使用的樣式。 (例如播放器的旋轉(zhuǎn)控件,只有一個頁面用到,只需要在Window級引用對應(yīng)資源字典) 不放在App.xaml原因是為了降低內(nèi)存消耗。 3.App.xaml 里面的資源引用適用于全局資源。理論上每一個被申明的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"> <!--應(yīng)用于全局的控件模板--> <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>
|
|