Reputation: 166
I am trying to add the style dynamically to button,below is the code i am using but it is not working for me
First Way
public Login()
{
InitializeComponent();
Style style = new Style
{
TargetType = typeof(Button)
};
style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.Red));
Resources.Add(typeof(Button), style);
}
second way one more thing i have tried with below second way also but it gives me error After a 'SetterBase' is in use (sealed), it cannot be modified
Style style = this.FindResource("ButtonStyle1") as Style;
Setter setter = (Setter)style.Setters[0];
setter.Property. = false;
Color orange1 = (Color)ColorConverter.ConvertFromString("#FFF3800C");
setter.Value = new SolidColorBrush(orange1);
Upvotes: 1
Views: 2786
Reputation: 10193
This article starts describes a solution, although it stops short of a detailed breakdown; you should find all you need by looking at the author's code here.
The main class is the ThemeManager
which provides a method to apply a theme to the application. The author represents a theme using a Theme
class - the important bit is the Uri
property. Essentially the ThemeManager
loads the appropriate resource file from the Uri, and adds it to the application's MergedDictionaries collection (removing any existing theme resource first).
So a condensed (and untested) version of the author's code might go something like this:-
private ResourceDictionary _currentTheme;
public void ApplyTheme(Uri uri)
{
if (_currentTheme != null)
{
// Unload the current theme.
Application.Current.Resources.MergedDictionaries.Remove(_currentTheme);
}
var resourceDictionary =
Application.LoadComponent(uri) as ResourceDictionary;
if (resourceDictionary != null)
{
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
_currentTheme = resourceDictionary;
}
}
An example of a Uri would be:
var uri = new Uri(
"/Wpf.TestHarness;component/Themes/RedTheme.xaml",
UriKind.Relative);
where Wpf.TestHarness
is the name of the compiled assembly (without the .DLL or .EXE extension) containing the XAML resource files, and /Themes/RedTheme.xaml' is the path to one of the XAML theme files embedded in that project (
component` is a required part of this "pack URI" path notation). Remember to set the build action of the XAML files in the project to "Page".
Now, create a style called "ButtonStyle1" in each of the theme files, but with different background colours for example. In your application I think you need to reference such styles using DynamicResource
(someone correct me if I'm wrong), e.g.:-
<Button Style="{DynamicResource ButtonStyle1}" ... />
Better still, don't name your styles. Just use the TargetType
, to apply it to every control of that type:-
<Style TargetType="{x:Type Button}">...</Style>
Good luck!
Upvotes: 0
Reputation: 18580
In your Windows Resources add
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Red"></Setter>
</Style>
</Window.Resources>
This will change background of all buttons in window
Upvotes: 1