开发者

multiple styles wpf from which user can choose

开发者 https://www.devze.com 2022-12-13 11:23 出处:网络
I want let the user choose what style my application has.Small example would be 2 buttons, if user presses button 1 then the background color turns red, if the user p开发者_高级运维resses button 2 the

I want let the user choose what style my application has.Small example would be 2 buttons, if user presses button 1 then the background color turns red, if the user p开发者_高级运维resses button 2 then the backgroundcolor turns green.

How do i do that? do i use multiple resource dictionaries? and apply them when the button is pressed? Whats the most common way of doing that?


As a very simple example:

 <Window.Resources>
    <Style TargetType="Window" x:Key="windowStyleOne">
        <Setter Value="123" Property="Content" />
        <Setter Value="Red" Property="Background"/>
    </Style>
    <Style TargetType="Window" x:Key="windowStyleTwo">
        <Setter Value="456" Property="Content" />
        <Setter Value="Green" Property="Background" />
    </Style>
</Window.Resources>

<Button Name ="myButtonOne"  Click="ButtonOne_Click">Red</Button>
<Button Name="myButtonTwo" Click="ButtonTwo_Click">Green</Button>


private void ButtonOne_Click(object sender, RoutedEventArgs e)
{   
   this.Style = (Style)(this.Resources["windowStyleOne"]);
}

private void ButtonTwo_Click(object sender, RoutedEventArgs e)
{
    this.Style = (Style)(this.Resources["windowStyleTwo"]);
}

EDIT: Actually heres an example that fits with your suggested example perfectly. myButtonOne makes the main window have a red background and myButtonTwo sets the window to have a green background.

0

精彩评论

暂无评论...
验证码 换一张
取 消