WPF XAML 为项目设置全局样式
生活随笔
收集整理的這篇文章主要介紹了
WPF XAML 为项目设置全局样式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
全局資源樣式屬性
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)一風格的所有控件。
? (例如按鈕,統(tǒng)一高寬,字體,字體大小,然后申明到獨立的資源字典中,
? ? 在App.xaml中引用)
2.頭部資源引用情況用于 不同 Window 適應不同主題或者風格的情況。
? ?比如為某一個窗口申明一個當前窗口單獨使用的樣式。
? (例如播放器的旋轉控件,只有一個頁面用到,只需要在Window級引用對應資源字典)
? ?不放在App.xaml原因是為了降低內(nèi)存消耗。
3.App.xaml 里面的資源引用適用于全局資源。理論上每一個被申明的Window
? ?都會創(chuàng)建一個對應資源字典的實例。除非是每個Window都會用到的模塊,
? ? 不然建議放到對應Window級
經(jīng)典實例:
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 <Applicationx: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>技術群:?需要進技術群學習交流的請?zhí)砑有【幬⑿?#xff0c;切記備注:加群,對以上內(nèi)容有什么疑問也可以直接和小編直接溝通交流!? ???
小編微信:mm1552923 ??
公眾號:dotNet編程大全? ?
總結
以上是生活随笔為你收集整理的WPF XAML 为项目设置全局样式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 198道K8sDocker面试真题大汇总
- 下一篇: 客户要求ASP.NET Core API