Click here to Skip to main content
15,890,123 members
Home / Discussions / WPF
   

WPF

 
QuestionFire EventTrigger on WPF Style Change? [SOLVED] Pin
AspDotNetDev21-Aug-09 11:20
protectorAspDotNetDev21-Aug-09 11:20 
AnswerRe: Fire EventTrigger on WPF Style Change? Pin
User 27100922-Aug-09 5:10
User 27100922-Aug-09 5:10 
GeneralRe: Fire EventTrigger on WPF Style Change? Pin
AspDotNetDev22-Aug-09 10:17
protectorAspDotNetDev22-Aug-09 10:17 
GeneralRe: Fire EventTrigger on WPF Style Change? Pin
User 27100922-Aug-09 12:22
User 27100922-Aug-09 12:22 
GeneralRe: Fire EventTrigger on WPF Style Change? Pin
AspDotNetDev22-Aug-09 17:23
protectorAspDotNetDev22-Aug-09 17:23 
GeneralRe: Fire EventTrigger on WPF Style Change? Pin
AspDotNetDev22-Aug-09 22:40
protectorAspDotNetDev22-Aug-09 22:40 
GeneralRe: Fire EventTrigger on WPF Style Change? Pin
User 27100923-Aug-09 4:43
User 27100923-Aug-09 4:43 
AnswerRe: Fire EventTrigger on WPF Style Change? [SOLUTION] Pin
AspDotNetDev23-Aug-09 15:09
protectorAspDotNetDev23-Aug-09 15:09 
Hmm, I'm not sure that will work, as you don't have access to the animation from the MyButton.OnStyleChanged method. Also, I'm not quite sure I like the idea of inheriting from every control that I want to be able to animate on a style switch. However, I got my technique coded and it ain't pretty, but it works. Here is the code in all its horrific glory:

The resource dictionary in "AnimatedStyleDictionary.xaml":
XML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1">
    <Style TargetType="Button">
        <Setter Property="Button.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <local:StyleChangedControl x:Name="MyStyleChangeCon" />
                        <Rectangle>
                            <Rectangle.Fill>
                                <SolidColorBrush x:Name="rectFill" Color="Yellow" />
                            </Rectangle.Fill>
                        </Rectangle>
                        <ContentPresenter Content="{TemplateBinding Button.Content}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <EventTrigger SourceName="MyStyleChangeCon" RoutedEvent="local:StyleChangedControl.StyleChange">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation
                                        Storyboard.TargetName="rectFill"
                                        Storyboard.TargetProperty="Color"
                                        To="Blue"
                                        AutoReverse="True"
                                        RepeatBehavior="Forever"
                                        Duration="0:0:1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>


The main window XAML (just a button really):
XML
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Button Click="Button_Click">Change Style</Button>
</Window>


The C# code behind the main window:
C#
// Main window.
public partial class Window1 : Window
{

	// Constructor.
	public Window1() {InitializeComponent();}

	// This button event changes the style.
	private void Button_Click(object sender, RoutedEventArgs e)
	{

		// Assign the new style.
		Application.Current.Resources.MergedDictionaries.Add(
			(ResourceDictionary)Application.LoadComponent(
			new Uri(@"AnimatedStyleDictionary.xaml", UriKind.Relative)));
		
		// Queue up a thread so the above style gets a
		// chance to apply before the below code executes.
		Thread t = new Thread(new ThreadStart(delegate()
		{
			this.Dispatcher.Invoke(new ThreadStart(delegate()
			{
				// Send notification that application style changed.
				StyleChangedControl.NotifyOfApplicationStyleChange();
			}));
		}));
		t.Start();

	}

}


The magical control that sends notifications of application style updates:
C#
// The sole purpose of this control is to fire the StyleChange event.
public class StyleChangedControl : Control
{

	// The static event.
	private static event ThreadStart ApplicationStyleChange;

	// The event that is on every instance of StyleChangedControl.
	public event RoutedEventHandler StyleChange
	{
		add
		{
			AddHandler(StyleChangeEvent, value);
		}
		remove
		{
			RemoveHandler(StyleChangeEvent, value);
		}
	}
	public static RoutedEvent StyleChangeEvent =
		EventManager.RegisterRoutedEvent("StyleChange",
		RoutingStrategy.Direct, typeof(RoutedEventHandler),
		typeof(StyleChangedControl));

	// Other classes call this when they change the application style.
	public static void NotifyOfApplicationStyleChange()
	{
		if (ApplicationStyleChange != null)
		{
			// Notify instances of style change.
			ApplicationStyleChange();
		}
	}

	// Constructor.
	public StyleChangedControl()
	{

		// This control does not need to be seen.
		this.Visibility = Visibility.Collapsed;

		// Subscribe instance method to static event.
		ApplicationStyleChange += this.OnStyleChange;

	}

	// Static event calls this.
	private void OnStyleChange()
	{

		// Raise event to notify all listening EventTriggers.
		RoutedEventArgs e = new RoutedEventArgs(StyleChangeEvent);
		RaiseEvent(e);

	}

}


Like I said, it ain't pretty, but it works. Since I figured out my own solution, I'm going to mark this as solved. Thanks for all your ideas, Karl!

Visual Studio is an excellent GUIIDE.

Questionservice Cross-domain issue. Pin
thrakazog21-Aug-09 8:33
thrakazog21-Aug-09 8:33 
AnswerResolved. Pin
thrakazog21-Aug-09 9:52
thrakazog21-Aug-09 9:52 
QuestionSheet music Pin
Tony Pottier21-Aug-09 5:58
Tony Pottier21-Aug-09 5:58 
AnswerRe: Sheet music Pin
#realJSOP21-Aug-09 7:12
mve#realJSOP21-Aug-09 7:12 
GeneralRe: Sheet music Pin
Tony Pottier21-Aug-09 10:59
Tony Pottier21-Aug-09 10:59 
GeneralRe: Sheet music Pin
Richard MacCutchan31-Aug-09 5:19
mveRichard MacCutchan31-Aug-09 5:19 
QuestionHow to handle Different Modules in MVP ?? Pin
Krishna Aditya20-Aug-09 22:50
Krishna Aditya20-Aug-09 22:50 
AnswerRe: How to handle Different Modules in MVP ?? Pin
Richard MacCutchan31-Aug-09 5:32
mveRichard MacCutchan31-Aug-09 5:32 
QuestionHow to stretch the image of (1* 66 ) pixel Pin
Krishna Aditya20-Aug-09 5:15
Krishna Aditya20-Aug-09 5:15 
AnswerRe: How to stretch the image of (1* 66 ) pixel Pin
Krishna Aditya20-Aug-09 5:53
Krishna Aditya20-Aug-09 5:53 
QuestionNavigation framework in tabs Pin
tcook81520-Aug-09 2:50
tcook81520-Aug-09 2:50 
AnswerRe: Navigation framework in tabs Pin
Michael Sync29-Aug-09 18:56
Michael Sync29-Aug-09 18:56 
QuestionHow to create a collapsible panel in WPF Pin
Krishna Aditya19-Aug-09 23:45
Krishna Aditya19-Aug-09 23:45 
AnswerRe: How to create a collapsible panel in WPF Pin
Pete O'Hanlon20-Aug-09 0:08
mvePete O'Hanlon20-Aug-09 0:08 
GeneralRe: How to create a collapsible panel in WPF Pin
abetterword29-Dec-10 9:20
abetterword29-Dec-10 9:20 
QuestionUDP & WCF Pin
Mohammad Dayyan19-Aug-09 22:00
Mohammad Dayyan19-Aug-09 22:00 
AnswerRe: UDP & WCF Pin
Pete O'Hanlon19-Aug-09 22:51
mvePete O'Hanlon19-Aug-09 22:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.