看了一篇博客,觉得样式很好看,就自己动手做了一下,做个总结。
效果:
选择不同的图片背景就会改变:
直接上代码:
每个Theme对应一张图,除了图的名称不同之外,Theme?.xaml中的内容相同:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ImageBrush x:Key="WindowBack" ImageSource="../Images/Theme1.jpg" Stretch="UniformToFill"/> </ResourceDictionary>
窗口的背景为动态资源,且有一个默认值在App.xaml中(<ResourceDictionary Source="Themes/Theme3.xaml"/>):
<Window x:Class="ChangeTheme.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="800" Width="800" WindowStyle="None" WindowState="Maximized" Background="{DynamicResource WindowBack}"> <Window.Resources> <ResourceDictionary> <Style x:Key="ScrollViewerStyle1" TargetType="{x:Type ScrollViewer}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ScrollViewer}"> <Grid> <Path Data="M101,0.5 L112,11.5 194.5,11.5 C197.26142,11.500001 199.5,13.738577 199.5,16.500001 L199.5,155.5 C199.5,158.26143 197.26142,160.5 194.5,160.5 L5.5,160.5 C2.7385788,160.5 0.5,158.26143 0.5,155.5 L0.5,16.500001 C0.5,13.738577 2.7385788,11.500001 5.5,11.5 L89.999999,11.5 z" Fill="White" Stretch="Fill" Stroke="SeaGreen" StrokeThickness="0.5"> <!--B27A7A7A--> <Path.Effect> <DropShadowEffect Opacity="0.7" ShadowDepth="2" Direction="310"/> </Path.Effect> </Path> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> </Window.Resources> <Grid Height="320" Width="400" HorizontalAlignment="Right" VerticalAlignment="Top"> <ScrollViewer Width="350" Style="{DynamicResource ScrollViewerStyle1}" > <WrapPanel Margin="15,50" x:Name="ImagePanel"> </WrapPanel> </ScrollViewer> </Grid> </Window>
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); LoadBackgroundImages(); } private void LoadBackgroundImages() { string path = @"../../Images"; var images =Directory.GetFiles(System.IO.Path.Combine(path)) ; if (images.Length == 0) return; foreach (var img in images) { Image imgTheme = new Image() { Width = 100, Stretch = Stretch.Fill, Height = 60, Margin = new Thickness(2) }; imgTheme.Name = System.IO.Path.GetFileNameWithoutExtension(img); imgTheme.Tag = img; imgTheme.Source = new BitmapImage(new Uri(img, UriKind.Relative)); imgTheme.MouseLeftButtonDown += new MouseButtonEventHandler(imgTheme_MouseLeftButtonDown); ImagePanel.Children.Add(imgTheme); } } private void imgTheme_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Image image = sender as Image; if (image != null && image.Source != null) { string imgName = image.Name; ResourceHelper.LoadResource("pack://application:,,,/ChangeTheme;component/Themes/" + imgName + ".xaml"); } } }
static class ResourceHelper { public static void LoadResource(string fileName) { try { Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary() { Source = new Uri(fileName, UriKind.RelativeOrAbsolute) }; } catch (Exception ex) { return; } } }