How to use Style.TargetType at Runtime
-
Friday, March 14, 2008 3:17 PM
Hi,
I tried to set style for all buttons in my StackPanel using TargetType property of Style object, but it didn't work.
Please Help me!
Code Snippet//Create Style
Style stl = new Style();
//Set Target Type
stl.TargetType = typeof(Button);//Create Setters
Setter setter = new Setter();
setter.Property = Button.VerticalAlignmentProperty;
setter.Value = VerticalAlignment.Bottom;stl.Setters.Add(setter);
//Another setters
// .....
/////////////////
//Create Button with stl Style, but button didn't get that style, Why? What is wrong?
Button b = new Button();
b.Content = i;
stackpanel.Children.Add(b);
Answers
-
Tuesday, March 18, 2008 4:07 AMModerator
You can use ResourceDictionary.Add method to do this. The main point is that the parameter key should be the type of button.
Resources.Add(typeof(Button), style);
Best Regards,
Wei Zhou
All Replies
-
Saturday, March 15, 2008 10:41 PMAre you putting the Style inside a ResourceDictionary?
like stackpanel.Resources.Add(stl); -
Monday, March 17, 2008 7:44 AMI tried this, but it didn't help. Also Resources takes two parameters - key and value.
I tried like this:
stackpanel.Resources.Add("ButtonStyle",stl);
Please Help! -
Monday, March 17, 2008 8:22 AMModerator
There are two possibilities may cause your problem.
1. The VerticalAlignment property has not effect when you use StackPanel as the button's container. You can read the following link about WPF layout system.
http://msdn2.microsoft.com/en-us/library/ms745058.aspx
2. The style has not applied on the button. The code below shows how to do this.
Code SnippetStyle style = new Style();
style.TargetType = typeof(Button);
Setter setter = new Setter(Button.MarginProperty, new Thickness(30));
style.Setters.Add(setter);
Button btn = new Button() { Content = "Button", Style = style };
((StackPanel)this.Content).Children.Add(btn);
Best Regards,
Wei Zhou
-
Monday, March 17, 2008 10:24 AMHi,
Yes, style is not applied to the concrete button, I want WPF to do that for me. I am trying to use Style.TargetType property for this, but it doesn't work.
Here are my full code:
Code Snippetusing System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
namespace Testing_Drawing_WPF
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
StackPanel stackpanel = new StackPanel();
Content = stackpanel;
stackpanel.Orientation = Orientation.Horizontal;
Style style = new Style();
style.TargetType = typeof(Button);
Setter setter = new Setter();
setter.Property = Button.VerticalAlignmentProperty;
setter.Value = VerticalAlignment.Bottom;
style.Setters.Add(setter);
setter = new Setter();
setter.Property = Button.LayoutTransformProperty;
ScaleTransform st = new ScaleTransform ();
setter.Value = st;
style.Setters.Add(setter);
EventTrigger eventtrigger = new EventTrigger();
eventtrigger.RoutedEvent = Button.MouseEnterEvent;
Storyboard storyboard = new Storyboard();
DoubleAnimation doubleanimation = new DoubleAnimation();
doubleanimation.To = 2;
doubleanimation.Duration = new TimeSpan(0, 0, 4);
DependencyProperty[] propertyChain1 =
new DependencyProperty[]
{
Button.LayoutTransformProperty,
ScaleTransform.ScaleXProperty
};
DependencyProperty[] propertyChain2 =
new DependencyProperty[]
{
Button.LayoutTransformProperty,
ScaleTransform.ScaleYProperty
};
string thePath = "(0).(1)";
Storyboard.SetTargetProperty(doubleanimation, new PropertyPath(thePath, propertyChain1));
Storyboard.SetTargetProperty(doubleanimation, new PropertyPath(thePath, propertyChain2));
storyboard.Children.Add(doubleanimation);
BeginStoryboard beginstoryboard = new BeginStoryboard();
beginstoryboard.Storyboard = storyboard;
eventtrigger.Actions.Add(beginstoryboard);
style.Triggers.Add(eventtrigger);
eventtrigger = new EventTrigger();
eventtrigger.RoutedEvent = Button.MouseLeaveEvent;
storyboard = new Storyboard();
doubleanimation = new DoubleAnimation();
doubleanimation.To = 1;
doubleanimation.Duration = new TimeSpan(0, 0, 4);
DependencyProperty[] propertyChain21 =
new DependencyProperty[]
{
Button.LayoutTransformProperty,
ScaleTransform.ScaleXProperty
};
DependencyProperty[] propertyChain22 =
new DependencyProperty[]
{
Button.LayoutTransformProperty,
ScaleTransform.ScaleYProperty
};
Storyboard.SetTargetProperty(doubleanimation, new PropertyPath(thePath, propertyChain21));
Storyboard.SetTargetProperty(doubleanimation, new PropertyPath(thePath, propertyChain22));
storyboard.Children.Add(doubleanimation);
beginstoryboard = new BeginStoryboard();
beginstoryboard.Storyboard = storyboard;
eventtrigger.Actions.Add(beginstoryboard);
style.Triggers.Add(eventtrigger);
for (int i = 0; i < 8; ++i )
{
Button btn = new Button();
btn.Content = i;btn.Style = style; // All works fine but I don't want to set for
each button it style, I want that WPF use
style "style" for each button in my app.
stackpanel.Children.Add(btn);
}
}
}
}
Please HELP! -
Tuesday, March 18, 2008 4:07 AMModerator
You can use ResourceDictionary.Add method to do this. The main point is that the parameter key should be the type of button.
Resources.Add(typeof(Button), style);
Best Regards,
Wei Zhou
-
Tuesday, March 18, 2008 7:19 AMThanks! It works!!!
-
Tuesday, June 24, 2008 6:28 AMThanks a lot.
I searching the same. very useful to me.
Thanks.Thanks.Thanks.Thanks.Thanks.
by
r
Yes! We can!!