我们先通过简单的效果展示,切换展示不同图片:
我们先定义图片资源文件,我们可以在window资源中定义,下面的在app.xaml文件来定义:
<Application x:Class="WPF异形窗口演示.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ImageBrush ImageSource="1.jpg" x:Key="key1"></ImageBrush> <ImageBrush ImageSource="2.jpg" x:Key="key2"></ImageBrush> </Application.Resources> </Application>
然后通过Combox控件来进行资源样式切换
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WPF异形窗口演示 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.comboBox1.Items.Add("样式1"); this.comboBox1.Items.Add("样式2"); this.comboBox1.Items.Add("样式3"); this.comboBox1.SelectedIndex = 0; } private void Grid_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { this.DragMove(); // this.Margin = new Thickness(10,10,10,10); } } private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (this.comboBox1.SelectedValue.ToString() == "样式1") { //通过uri指向图片位置 this.Background = new ImageBrush(new System.Windows.Media.Imaging.BitmapImage(new Uri("3.jpg", UriKind.Relative))); ; } else if (this.comboBox1.SelectedValue.ToString() == "样式2") { //通过资源文件获取 this.Background = (ImageBrush)Application.Current.Resources["key1"]; } else if (this.comboBox1.SelectedValue.ToString() == "样式3") { this.Background = (ImageBrush)Application.Current.Resources["key2"]; } } } }
我们可以通过这样的方式来动态实现不同效果展示
示例小demo: