locked
Help with wpf and c# custom controls RRS feed

  • Question

  • Hi there...
    I have a problem in my application and don't know how to solve... Here it is: I have build a custom control that have several other controls inside of it (like buttons, textBoxes, etc.), and this custom control is placed in a window along others custom controls. But how do I trigger a animation in the window from a custom control button event? And how to do that in another custom control?
    If I didn't make myself clear, I want to a button click event inside a custom control initiates a animation outside of it... Is it possible?
    Thanks in advance...
    Friday, October 31, 2008 6:44 PM

Answers

  • Because wpf introduces routed events you can try something like this.

    In the window

        <Grid> 
            <Grid.Background> 
                <SolidColorBrush x:Name="brush" Color="Transparent" /> 
            </Grid.Background> 
            <StackPanel> 
                <StackPanel.Triggers> 
                    <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button1">  
                        <BeginStoryboard > 
                            <Storyboard > 
                                <ColorAnimation   Storyboard.TargetName="brush" 
                                                  Storyboard.TargetProperty="Color" 
                                                  To="Yellow" Duration="0:0:2" /> 
                            </Storyboard> 
                        </BeginStoryboard> 
                    </EventTrigger> 
                </StackPanel.Triggers> 
                <local:UserControl1 /> 
            </StackPanel> 
        </Grid> 

    In the usercontrol
        <Grid> 
            <StackPanel> 
                <Button x:Name="button1">Click me</Button> 
            </StackPanel> 
        </Grid> 
    • Proposed as answer by KCSharp Friday, October 31, 2008 8:04 PM
    • Marked as answer by Hua Chen Thursday, November 6, 2008 1:24 PM
    Friday, October 31, 2008 8:04 PM

All replies

  • Because wpf introduces routed events you can try something like this.

    In the window

        <Grid> 
            <Grid.Background> 
                <SolidColorBrush x:Name="brush" Color="Transparent" /> 
            </Grid.Background> 
            <StackPanel> 
                <StackPanel.Triggers> 
                    <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="button1">  
                        <BeginStoryboard > 
                            <Storyboard > 
                                <ColorAnimation   Storyboard.TargetName="brush" 
                                                  Storyboard.TargetProperty="Color" 
                                                  To="Yellow" Duration="0:0:2" /> 
                            </Storyboard> 
                        </BeginStoryboard> 
                    </EventTrigger> 
                </StackPanel.Triggers> 
                <local:UserControl1 /> 
            </StackPanel> 
        </Grid> 

    In the usercontrol
        <Grid> 
            <StackPanel> 
                <Button x:Name="button1">Click me</Button> 
            </StackPanel> 
        </Grid> 
    • Proposed as answer by KCSharp Friday, October 31, 2008 8:04 PM
    • Marked as answer by Hua Chen Thursday, November 6, 2008 1:24 PM
    Friday, October 31, 2008 8:04 PM
  • Oh... thank you so much... works fine... But would you know how to do this by c# code?
    Saturday, November 1, 2008 5:27 PM