WPF換膚設計道理淺析。本站提示廣大學習愛好者:(WPF換膚設計道理淺析)文章只能為提供參考,不一定能成為您想要的結果。以下是WPF換膚設計道理淺析正文
WPF換膚的設計道理,應用資本字典為每種皮膚資本添加分歧的款式,在後台切換皮膚資本文件。
截圖
上圖中,第一張圖采取規矩款式,第二張圖采取不規矩款式,截圖的時刻略有瑕疵。
資本字典
規矩款式資本Skin.RegularStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!--Window款式--> <Style x:Key="WindowStyle" TargetType="Window"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Window"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Border.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="Green" Offset="0"></GradientStop> <GradientStop Color="LightGreen" Offset="0.4"></GradientStop> <GradientStop Color="White" Offset="1"></GradientStop> </LinearGradientBrush> </Border.Background> <ContentPresenter></ContentPresenter> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--Button款式--> <Style TargetType="Button"> <Setter Property="Width" Value="70"></Setter> <Setter Property="Height" Value="23"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Name="bdr" Cursor="Arrow" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Border.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="White" Offset="0"></GradientStop> <GradientStop Color="LightGreen" Offset="0.3"></GradientStop> <GradientStop Color="Green" Offset="1"></GradientStop> </LinearGradientBrush> </Border.Background> <TextBlock Name="tbk" Background="Transparent" Foreground="DarkGreen" TextAlignment="Center" Text="{TemplateBinding Content}"></TextBlock> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="bdr" Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="LightGreen" Offset="0"></GradientStop> <GradientStop Color="Green" Offset="1"></GradientStop> </LinearGradientBrush> </Setter.Value> </Setter> <Setter TargetName="tbk" Property="Foreground" Value="White"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--TextBox款式--> <Style TargetType="TextBox"> <Setter Property="FontFamily" Value="SketchFlow Print"/> <Setter Property="FontSize" Value="14"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TextBox"> <Border BorderBrush="DarkGreen" BorderThickness="0.5"> <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"></ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--ContextMenu款式--> <Style TargetType="ContextMenu"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ContextMenu"> <Border BorderBrush="Green" BorderThickness="1"> <ItemsPresenter/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--MenuItem款式--> <Style TargetType="MenuItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="MenuItem"> <Border Name="border" Background="LightGreen" BorderThickness="0"> <TextBlock Name="tbk" Background="Transparent" Padding="5,5" Text="{TemplateBinding Header}"></TextBlock> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="Green"></Setter> <Setter TargetName="tbk" Property="Foreground" Value="White"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--TextBlock款式--> <Style TargetType="TextBlock"> <Setter Property="FontFamily" Value="SketchFlow Print"/> <Setter Property="FontSize" Value="14"/> </Style> </ResourceDictionary> 不規矩款式資本Skin.RoundedCornerStyle.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!--Window款式--> <Style x:Key="WindowStyle" TargetType="Window"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Window"> <Grid Margin="10"> <Rectangle Fill="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" RadiusX="5" RadiusY="5"> <Rectangle.Effect> <DropShadowEffect BlurRadius="10" Color="Black" Direction="0" Opacity="0.8" RenderingBias="Performance" ShadowDepth="0"/> </Rectangle.Effect> </Rectangle> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True" CornerRadius="5"> <Border.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="Blue" Offset="0"></GradientStop> <GradientStop Color="LightBlue" Offset="0.4"></GradientStop> <GradientStop Color="White" Offset="1"></GradientStop> </LinearGradientBrush> </Border.Background> <ContentPresenter></ContentPresenter> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--Button款式--> <Style TargetType="Button"> <Setter Property="Width" Value="70"></Setter> <Setter Property="Height" Value="23"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Name="bdr" CornerRadius="5" Cursor="Hand" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <TextBlock Name="tbk" Background="Transparent" Foreground="Yellow" TextAlignment="Center" Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Content}"></TextBlock> <Border.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="White" Offset="0"></GradientStop> <GradientStop Color="LightBlue" Offset="0.3"></GradientStop> <GradientStop Color="Blue" Offset="1"></GradientStop> </LinearGradientBrush> </Border.Background> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="bdr" Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="LightBlue" Offset="0"></GradientStop> <GradientStop Color="Blue" Offset="1"></GradientStop> </LinearGradientBrush> </Setter.Value> </Setter> <Setter TargetName="tbk" Property="Foreground" Value="LightYellow"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--TextBox款式--> <Style TargetType="TextBox"> <Setter Property="FontFamily" Value="Times New Roman"></Setter> <Setter Property="FontSize" Value="14"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="TextBox"> <Border BorderBrush="Blue" BorderThickness="0.5" CornerRadius="5"> <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"></ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--ContextMenu款式--> <Style TargetType="ContextMenu"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ContextMenu"> <Border CornerRadius="5" BorderBrush="Blue" BorderThickness="1"> <ItemsPresenter/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--MenuItem款式--> <Style TargetType="MenuItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="MenuItem"> <Border Name="border" Background="LightSkyBlue" BorderThickness="0" CornerRadius="5"> <TextBlock Name="tbk" Background="Transparent" Padding="5,5" Text="{TemplateBinding Header}"></TextBlock> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="border" Property="Background" Value="BlueViolet"></Setter> <Setter TargetName="tbk" Property="Foreground" Value="White"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--TextBlock款式--> <Style TargetType="TextBlock"> <Setter Property="FontFamily" Value="Times New Roman"/> <Setter Property="FontSize" Value="14"/> </Style> </ResourceDictionary>
細心不雅察下面界說的款式,你會發明在界說Window款式的時刻指定了Key,其他的Control款式卻沒有指定Key。年夜家都曉得,假如沒有給Style指定Key,那末這個Style會運用到一切目的類型(TargetType)為指定類型的Control。請看上面一段文字:
由於在換膚的進程中,須要靜態加載Window的款式,所以用DynamicResource作綁定。
App.xaml
法式運轉的時刻,默許加載規矩款式的皮膚。
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Dictionary\Skin.RegularStyle.xaml"></ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
後台代碼
/// <summary> /// MenuItem的履行辦法 /// </summary> /// <param name="parameter"></param> private void RelayMenuItemEvent(object parameter) { if (parameter.ToString() == RegularStyle) { ChangeSkinResource(Skins[0]); } else if (parameter.ToString() == RoundedCornerStyle) { ChangeSkinResource(Skins[1]); } } /// <summary> /// 改換皮膚資本 /// </summary> /// <param name="skin"></param> private void ChangeSkinResource(ResourceDictionary skin) { if (Application.Current.Resources.MergedDictionaries[0].Source.IsAbsoluteUri) { if (Application.Current.Resources.MergedDictionaries[0].Source.OriginalString != skin.Source.OriginalString) { Application.Current.Resources.MergedDictionaries[0] = skin; } } else { if (Application.Current.Resources.MergedDictionaries[0].Source.OriginalString.ToString('\\') != skin.Source.OriginalString.ToString('/')) { Application.Current.Resources.MergedDictionaries[0] = skin; } } }
運轉的時刻在MainWindow上右鍵選擇皮膚款式,便可以換膚了。
源碼下載:http://xiazai.jb51.net/201610/yuanma/WPFSkin(jb51.net).rar
鏈接:stackoverflow
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。