Click here to Skip to main content

Silverlight / WPF

    RSS: RSS Feed

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page  Show 
  Refresh
AnswerRe: Wpf listbox select item Pinmembermark01127:32 22 Aug '09  
GeneralRe: Wpf listbox select item PinmvpChristian Graus15:25 22 Aug '09  
GeneralRe: Wpf listbox select item [modified] Pinmembermark011216:10 22 Aug '09  
GeneralRe: Wpf listbox select item Pinmemberaspdotnetdev23:44 22 Aug '09  
QuestionEditing multiple item types in the same control Pinmembergantww17:43 21 Aug '09  
AnswerRe: Editing multiple item types in the same control PinmvpMark Salsbery9:43 24 Aug '09  
QuestionFire EventTrigger on WPF Style Change? [SOLVED] Pinmemberaspdotnetdev12:20 21 Aug '09  
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="0Blush | :O :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 Shifflett6:10 22 Aug '09  
GeneralRe: Fire EventTrigger on WPF Style Change? Pinmemberaspdotnetdev11:17 22 Aug '09  
GeneralRe: Fire EventTrigger on WPF Style Change? PinmvpKarl Shifflett13:22 22 Aug '09  
GeneralRe: Fire EventTrigger on WPF Style Change? Pinmemberaspdotnetdev18:23 22 Aug '09  
GeneralRe: Fire EventTrigger on WPF Style Change? Pinmemberaspdotnetdev23:40 22 Aug '09  
GeneralRe: Fire EventTrigger on WPF Style Change? PinmvpKarl Shifflett5:43 23 Aug '09  
AnswerRe: Fire EventTrigger on WPF Style Change? [SOLUTION] Pinmemberaspdotnetdev16:09 23 Aug '09  
Questionservice Cross-domain issue. Pinmemberthrakazog9:33 21 Aug '09  
AnswerResolved. Pinmemberthrakazog10:52 21 Aug '09  
QuestionSheet music PinmemberTony_P6:58 21 Aug '09  
AnswerRe: Sheet music PinmvpJohn Simmons / outlaw programmer8:12 21 Aug '09  
GeneralRe: Sheet music PinmemberTony_P11:59 21 Aug '09  
GeneralRe: Sheet music PinmemberRichard MacCutchan6:19 31 Aug '09  
QuestionHow to handle Different Modules in MVP ?? PinmemberKrishna Aditya23:50 20 Aug '09  
AnswerRe: How to handle Different Modules in MVP ?? PinmemberRichard MacCutchan6:32 31 Aug '09  
QuestionHow to stretch the image of (1* 66 ) pixel PinmemberKrishna Aditya6:15 20 Aug '09  
AnswerRe: How to stretch the image of (1* 66 ) pixel PinmemberKrishna Aditya6:53 20 Aug '09  
QuestionNavigation framework in tabs Pinmembertcook8153:50 20 Aug '09  

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.5.120210.1 | Last Updated 13 Feb 2012
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid