Click here to Skip to main content
15,886,724 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Wpf listbox select item Pin
User 27100922-Aug-09 5:11
User 27100922-Aug-09 5:11 
AnswerRe: Wpf listbox select item Pin
mark011222-Aug-09 6:32
mark011222-Aug-09 6:32 
GeneralRe: Wpf listbox select item Pin
Christian Graus22-Aug-09 14:25
protectorChristian Graus22-Aug-09 14:25 
GeneralRe: Wpf listbox select item [modified] Pin
mark011222-Aug-09 15:10
mark011222-Aug-09 15:10 
GeneralRe: Wpf listbox select item Pin
AspDotNetDev22-Aug-09 22:44
protectorAspDotNetDev22-Aug-09 22:44 
QuestionEditing multiple item types in the same control Pin
gantww21-Aug-09 16:43
gantww21-Aug-09 16:43 
AnswerRe: Editing multiple item types in the same control Pin
Mark Salsbery24-Aug-09 8:43
Mark Salsbery24-Aug-09 8:43 
QuestionFire EventTrigger on WPF Style Change? [SOLVED] Pin
AspDotNetDev21-Aug-09 11:20
protectorAspDotNetDev21-Aug-09 11: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:
XML
<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.
XML
<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):
C#
// 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:
C#
// 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? 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 
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 

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.