标签:style blog http color 使用 os io 文件
假设一个应用程序中,某个窗口需要使用样式,但是样式非常多,写在一个窗口中代码分类不方便。最好Style写在专门的xaml文件中,然后引用到窗口中,就像HTML引用外部css文件一样。
实现方法:
1、创建新建项->添加/资源字典 Style.xaml,并添加Style样式。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!--BaseStyle--> <Style x:Key="BaseStyle" TargetType="{x:Type Control}"> <Setter Property="Margin" Value="5"/> </Style> <!--ButtonStyle--> <Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}"> <Setter Property="Width" Value="80"/> <Setter Property="Height" Value="40"/> <Setter Property="Background" Value="Blue"/> </Style> <!--GroupStyle--> <Style TargetType="{x:Type GroupBox}" BasedOn="{StaticResource BaseStyle}"></Style> <!--TextBoxStyle--> <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseStyle}"> </Style> <!--CheckBox--> <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseStyle}"> </Style> <!--ListBox--> <Style TargetType="{x:Type ListBox}" BasedOn="{StaticResource BaseStyle}"> </Style> <!--ProgressBar--> <Style TargetType="{x:Type ProgressBar}" BasedOn="{StaticResource BaseStyle}"> </Style> <!--TextBlock--> <Style TargetType="{x:Type TextBlock}" > <Setter Property="Margin" Value="5" /> </Style> </ResourceDictionary>
2、在窗口中引用外部资源。注意:在Window中添加外部样式需要指定key,而Application中则不需要,所以这里baseResourceDictionaryStyle就是引用外部样式Style.xaml的key。
<Window x:Class="WpfApplication1.ResourceDictionary.WinResourceKey" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WinResourceKey" Height="300" Width="300"> <Window.Resources> <ResourceDictionary > <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Style.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid> </Grid> </Window>
3、将样式应用到窗体的布局上。
<Window x:Class="WpfApplication1.ResourceDictionary.WinResourceKey" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WinResourceKey" Height="300" Width="300"> <Window.Resources> <ResourceDictionary > <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Style.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <Grid> <Button Content="ABC" > </Button> </Grid> </Window>
参考:http://www.cnblogs.com/therock/articles/2135997.html
标签:style blog http color 使用 os io 文件
原文地址:http://www.cnblogs.com/zhaox583132460/p/3905843.html