Click here to Skip to main content
11,411,922 members (63,072 online)

Silverlight / WPF

 
AnswerRe: Wpf listbox select item PinmvpKarl Shifflett22-Aug-09 6:11 
AnswerRe: Wpf listbox select item Pinmembermark011222-Aug-09 7:32 
GeneralRe: Wpf listbox select item PinmvpChristian Graus22-Aug-09 15:25 
GeneralRe: Wpf listbox select item [modified] Pinmembermark011222-Aug-09 16:10 
GeneralRe: Wpf listbox select item Pinmemberaspdotnetdev22-Aug-09 23:44 
QuestionEditing multiple item types in the same control Pinmembergantww21-Aug-09 17:43 
AnswerRe: Editing multiple item types in the same control PinmvpMark Salsbery24-Aug-09 9:43 
QuestionFire EventTrigger on WPF Style Change? [SOLVED] Pinmemberaspdotnetdev21-Aug-09 12:20 
This is one for you WPF gurus. Basically, I want a button to pulsate continuously (i.e., fade from one color to another and back, then repeat forever). I've got that working, but it does not work under another condition. I also want to be able to change styles at runtime. I have the runtime style changing working and I have the pulsating button working, but not both at the same time. The problem seems to be that I'm using Button.Loaded as the event trigger that begins the storyboard. However, if I swap the style after the button has already loaded, that storyboard never starts, so the animation doesn't execute. I was thinking there was perhaps another event I could use (such as an "on style switch" event), but I'm really at a loss. Here is some simple code to help you understand the problem:

First, I have a resource dictionary stored in "AnimatedStyleDictionary.xaml". This contains the style for the pulsating button, as shown below:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button">
        <Setter Property="Button.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <Rectangle>
                            <Rectangle.Fill>
                                <SolidColorBrush x:Name="rectFill" Color="Yellow" />
                            </Rectangle.Fill>
                        </Rectangle>
                        <ContentPresenter Content="{TemplateBinding Button.Content}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="Button.Loaded">
                            <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>


Next, I have a Window with a button on it.
<Window x:Class="CodeProjectSample_RuntimeSwapAnimatedStyle.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">Hello</Button>
</Window>


When that button gets clicked, the application style gets switched (I know this is a poor technique, but I wanted to trim the code to a minimum for this sample):
// Button got clicked.
private void Button_Click(object sender, RoutedEventArgs e)
{
	Application.Current.Resources.MergedDictionaries.Add((ResourceDictionary)Application.LoadComponent(new Uri(@"AnimatedStyleDictionary.xaml", UriKind.Relative)));
}


The above code is where the failure occurs. The style gets applied (I can confirm because the button turns yellow), but the animation does not fire. Also, I have confirmed that the animation does work. If I modify the code to load the style in the Window constructor rather than the button, the animation works fine. However, that is not a working solution, as I want the user to be able to change between styles at runtime. Here is the code that causes the animation to work:
// Window constructor.
public Window1()
{
	Application.Current.Resources.MergedDictionaries.Add((ResourceDictionary)Application.LoadComponent(new Uri(@"AnimatedStyleDictionary.xaml", UriKind.Relative)));
	InitializeComponent();
}

Any help you could provide would be greatly appreciated!

Learn as if you would live forever, live as if you would die tomorrow.
modified on Thursday, September 3, 2009 6:29 PM

AnswerRe: Fire EventTrigger on WPF Style Change? PinmvpKarl Shifflett22-Aug-09 6:10 
GeneralRe: Fire EventTrigger on WPF Style Change? Pinmemberaspdotnetdev22-Aug-09 11:17 
GeneralRe: Fire EventTrigger on WPF Style Change? PinmvpKarl Shifflett22-Aug-09 13:22 
GeneralRe: Fire EventTrigger on WPF Style Change? Pinmemberaspdotnetdev22-Aug-09 18:23 
GeneralRe: Fire EventTrigger on WPF Style Change? Pinmemberaspdotnetdev22-Aug-09 23:40 
GeneralRe: Fire EventTrigger on WPF Style Change? PinmvpKarl Shifflett23-Aug-09 5:43 
AnswerRe: Fire EventTrigger on WPF Style Change? [SOLUTION] Pinmemberaspdotnetdev23-Aug-09 16:09 
Questionservice Cross-domain issue. Pinmemberthrakazog21-Aug-09 9:33 
AnswerResolved. Pinmemberthrakazog21-Aug-09 10:52 
QuestionSheet music PinmemberTony_P21-Aug-09 6:58 
AnswerRe: Sheet music PinmvpJohn Simmons / outlaw programmer21-Aug-09 8:12 
GeneralRe: Sheet music PinmemberTony_P21-Aug-09 11:59 
GeneralRe: Sheet music PinmemberRichard MacCutchan31-Aug-09 6:19 
QuestionHow to handle Different Modules in MVP ?? PinmemberKrishna Aditya20-Aug-09 23:50 
AnswerRe: How to handle Different Modules in MVP ?? PinmemberRichard MacCutchan31-Aug-09 6:32 
QuestionHow to stretch the image of (1* 66 ) pixel PinmemberKrishna Aditya20-Aug-09 6:15 
AnswerRe: How to stretch the image of (1* 66 ) pixel PinmemberKrishna Aditya20-Aug-09 6:53 

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

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


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 22 Apr 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid