Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

c# - Changing the styles at runtime in WPF

I am trying to allow the user to customize the elements in a WPF application. What I am trying to achieve is, if I have a list box which specifies all the form elements (TextBox, label etc.) user can pick one form element, and set the style property say Label, foreground should be in orange where as for TextBox foreground should be in black and so on. And as per what ever style I am intending to apply all the TextBoxes should look alike.

I am not able to go figure out a way for achieving this. I have tried out an example where multiple pre-defined styles can be uploaded at runtime. So now, I would like to find a way of changing the property of different elements at runtime.

UPDATE:

I tried to create a new style from the code behind.

XAML

<Label Content="SAMPLE" Style="{DynamicResource Style1}" x:Name="label1" />
<Button Content="Button" Click="Button_Click" />

and in code behind i.e. on click of the Button I tried this:

Style style = new Style { TargetType = typeof(Label) };
style.Setters.Add(new Setter(Control.ForegroundProperty, Brushes.Black));
Application.Current.Resources["Style1"] = style;

But it is not getting updated.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to make sure that the styles are in the file App.xaml:

<Application x:Class="ChangeStyleHelp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">

    <Application.Resources>
        <Style x:Key="MyStyle" TargetType="{x:Type Label}">
            <Setter Property="Background" Value="Green" />
        </Style>
    </Application.Resources>
</Application>

Code behind:

private void ChangeStyle_Click(object sender, RoutedEventArgs e)
{
    Style style = new Style 
    { 
        TargetType = typeof(Label) 
    };

    style.Setters.Add(new Setter(Label.BackgroundProperty, Brushes.Aquamarine));

    Application.Current.Resources["MyStyle"] = style;
}   

If the Style is in the resource of Window (Window.Resources), then you need to write this, or the name of the Window:

private void ChangeStyle_Click(object sender, RoutedEventArgs e)
{
    Style style = new Style 
    { 
        TargetType = typeof(Label) 
    };

    style.Setters.Add(new Setter(Label.BackgroundProperty, Brushes.Aquamarine));

    this.Resources["MyStyle"] = style;
}   

As well, you can change the Style this way: take an existing style and use of the element. Example:

<Application x:Class="ChangeStyleHelp.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">

    <Application.Resources>
        <Style x:Key="AnotherWayStyle" TargetType="{x:Type Label}">
            <Setter Property="Background" Value="Lavender" />
            <Setter Property="Foreground" Value="OrangeRed" />
        </Style>
    </Application.Resources>
</Application>  

Code behind:

private void AnotherWay_Click(object sender, RoutedEventArgs e)
{
    label1.Style = (Style)Application.Current.Resources["AnotherWayStyle"];
}   

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...