先看效果图
<Window x:Class="WpfApp1.FragmentWindow"
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:WpfApp1"
mc:Ignorable="d"
Name="FragmentWindow1"
Title="FragmentWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="One" TargetType="{x:Type Grid}">
<Style.Setters>
<Setter Property="Background" Value="AliceBlue"></Setter>
</Style.Setters>
</Style>
<Style x:Key="Two" TargetType="{x:Type Grid}">
<Style.Setters>
<Setter Property="Background" Value="Black"></Setter>
</Style.Setters>
</Style>
<Style x:Key="Three" TargetType="{x:Type Grid}">
<Style.Setters>
<Setter Property="Background" Value="Red"></Setter>
</Style.Setters>
</Style>
<Style x:Key="Four" TargetType="{x:Type Grid}">
<Style.Setters>
<Setter Property="Background" Value="Yellow"></Setter>
</Style.Setters>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="111"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="One" Name="btnOne" Click="BtnOne_Click"></Button>
<Button Grid.Row="1" Content="Two" Name="btnTwo" Click="BtnTwo_Click"></Button>
<Button Grid.Row="2" Content="Three" Name="btnThree" Click="BtnThree_Click"></Button>
<Button Grid.Row="3" Content="Four" Name="btnFour" Click="BtnFour_Click"></Button>
</Grid>
<Grid Grid.Column="1" Name="gridStyle">
</Grid>
</Grid>
</Window>
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace WpfApp1
{
public partial class FragmentWindow : Window
{
public FragmentWindow()
{
InitializeComponent();
}
private void BtnOne_Click(object sender, RoutedEventArgs e)
{
var style = this.TryFindResource(GridStyle.One.ToString()) as Style;
if (style != null)
{
gridStyle.Style = style;
}
}
private void BtnTwo_Click(object sender, RoutedEventArgs e)
{
var style = this.TryFindResource(GridStyle.Two.ToString()) as Style;
if (style != null)
{
gridStyle.Style = style;
}
}
private void BtnThree_Click(object sender, RoutedEventArgs e)
{
var style = this.TryFindResource(GridStyle.Three.ToString()) as Style;
if (style != null)
{
gridStyle.Style = style;
}
}
private void BtnFour_Click(object sender, RoutedEventArgs e)
{
var style = this.TryFindResource(GridStyle.Four.ToString()) as Style;
if (style != null)
{
gridStyle.Style = style;
}
}
}
public enum GridStyle
{
One = 1,
Two,
Three,
Four
}
}
参考:
IValueConverter Example and Usage in WPF
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1.style">
<Style x:Key="One" TargetType="{x:Type Grid}">
<Style.Setters>
<Setter Property="Background" Value="AliceBlue"></Setter>
</Style.Setters>
</Style>
<Style x:Key="Two" TargetType="{x:Type Grid}">
<Style.Setters>
<Setter Property="Background" Value="AntiqueWhite"></Setter>
</Style.Setters>
</Style>
<Style x:Key="Three" TargetType="{x:Type Grid}">
<Style.Setters>
<Setter Property="Background" Value="Aqua"></Setter>
</Style.Setters>
</Style>
<Style x:Key="Four" TargetType="{x:Type Grid}">
<Style.Setters>
<Setter Property="Background" Value="Aquamarine"></Setter>
</Style.Setters>
</Style>
</ResourceDictionary>
<Window x:Class="WpfApp1.FragmentWindowByConverter"
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:WpfApp1"
mc:Ignorable="d"
Title="FragmentWindowByConverter" Height="450" Width="800"
Name ="FragmentWindowByConverterName">
<Window.Resources>
<local:FragmentWindowConverter x:Key="FWC"/>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="111"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<RadioButton Grid.Row="0" Width="50" Height="50" VerticalContentAlignment="Center" Content="One" Name="btnOne" IsChecked="{Binding ElementName=gridStyle,Path=Style,Converter={StaticResource FWC},ConverterParameter=One}"></RadioButton>
<RadioButton Grid.Row="1" Width="50" Height="50" VerticalContentAlignment="Center" Content="Two" Name="btnTwo" IsChecked="{Binding ElementName=gridStyle,Path=Style,Converter={StaticResource FWC},ConverterParameter=Two}"></RadioButton>
<RadioButton Grid.Row="2" Width="50" Height="50" VerticalContentAlignment="Center" Content="Three" Name="btnThree" IsChecked="{Binding ElementName=gridStyle,Path=Style,Converter={StaticResource FWC},ConverterParameter=Three}"></RadioButton>
<RadioButton Grid.Row="3" Width="50" Height="50" VerticalContentAlignment="Center" Content="Four" Name="btnFour" IsChecked="{Binding ElementName=gridStyle,Path=Style,Converter={StaticResource FWC},ConverterParameter=Four}"></RadioButton>
</Grid>
<Grid Grid.Column="2" Name="gridStyle"/>
</Grid>
</Window>
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
namespace WpfApp1
{
public partial class FragmentWindowByConverter : Window
{
public FragmentWindowByConverter()
{
InitializeComponent();
}
public enum TypeFragmentWindowStyle
{
One = 1,
Two,
Three,
Four
}
}
public class FragmentWindowConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Maybe(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Maybe(parameter);
}
private object Maybe(object obj)
{
if (obj != null)
{
Style style = Application.Current.TryFindResource(obj.ToString()) as Style;
return style;
}
return Binding.DoNothing;
}
}
}