前台
<Window x:Class="SkinnedApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SkinnedApplication"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Button Name="newChildWindowButton" Click="newChildWindowButton_Click">New ChildWindow</Button>
<StackPanel Orientation="Horizontal">
<Label>Skin:</Label>
<ComboBox Name="skinComboBox" />
</StackPanel>
</StackPanel>
</Window>
后台
using System.Windows;
using System.Windows.Controls;
namespace SkinnedApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Add choices to combo box
skinComboBox.Items.Add("Blue");
skinComboBox.Items.Add("Yellow");
skinComboBox.SelectedIndex = 0;
// Set initial skin
Application.Current.Resources = (ResourceDictionary) Application.Current.Properties["Blue"];
// Detect when skin changes
skinComboBox.SelectionChanged += skinComboBox_SelectionChanged;
}
private void newChildWindowButton_Click(object sender, RoutedEventArgs e)
{
// Create a new skind child window
var window = new ChildWindow();
window.Show();
}
private void skinComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Change the skin
var selectedValue = (string) e.AddedItems[0];
Application.Current.Resources = (ResourceDictionary) Application.Current.Properties[selectedValue];
}
}
}