Trying to move from Forms to WPF and need a little help

Tre4b 136 Reputation points
2020-12-14T13:55:18.273+00:00

Okay I want to do some animation in WPF to move something in from the right of the screen.

In xaml I do this

<UserControl x:Class="testproject.myControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:testproject"
mc:Ignorable="d"
>
<Grid x:Name="mygrid">
<Image x:Name="myImage" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Stretch="Uniform">
<Image.Triggers>
<EventTrigger RoutedEvent="Image.Loaded" >
<BeginStoryboard>
<Storyboard>
<ThicknessAnimation Name="Move" BeginTime="0:0:0" From="500,0,-500,0" To="0,0,0,0" Duration="0:0:7" Storyboard.TargetProperty="(Margin)" DecelerationRatio="0.9"></ThicknessAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Image.Triggers>
</Image>
</Grid>
</UserControl>

And it works perfectly. Now that's great but I want to vary some of the parameters and do a reverse sometime later in the code. Was easy enough to trigger this on load.

I thought, okay I will transfer this to a function in the .cs file for this Control. Therefore when the function is called the control will do what it is told.

    public void makeanim()
    {
        var sb = new Storyboard();
        var SlideAnimation = new ThicknessAnimation
        {
            Duration = new Duration(TimeSpan.FromSeconds(10)),
            From = new Thickness(500, 0, -500, 0),
            To = new Thickness(0),
            DecelerationRatio = 0.9f
        };
        Storyboard.SetTargetName(SlideAnimation, this.Name);
        Storyboard.SetTargetProperty(SlideAnimation, new PropertyPath("Margin"));
        sb.Children.Add(SlideAnimation);

        sb.Begin();
    }

No matter what I do I cannot figure out how to get this to target the Margin on the control. What I get when the code runs is

System.InvalidOperationException: 'No applicable name scope exists to resolve the name 'mycontrol'.

I've tried various other things, variations of Target Name and TargetProperty but all fail to compile or come up with similar issues on execution. Have been looking for a clear guide (though I did follow the MS one through using my control rather than their rectangle). Nothing seems to work so I suspect I have a fundemental mis-understanding going on here. Can anyone point me in the right direction?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,666 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2020-12-15T07:04:59.51+00:00

    I update xaml code for myControl.xaml:

     <Grid x:Name="mygrid">  
            <Image Name="myImage" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Stretch="Uniform"  Source="/1.jpg">  
            </Image>  
        </Grid>  
    

    The code for myControl.xaml.cs:

    public MyControl()  
            {  
                InitializeComponent();  
                makeanim();  
      
            }  
      
            private void makeanim()  
            {  
                Storyboard sb = new Storyboard();  
                ThicknessAnimation SlideAnimation = new ThicknessAnimation();  
                SlideAnimation.Duration = TimeSpan.FromMilliseconds(1000);  
                SlideAnimation.From = new Thickness(500, 0, -500, 0);  
                SlideAnimation.To = new Thickness(0, 0, 0, 0);  
                SlideAnimation.BeginTime = TimeSpan.Zero;  
                SlideAnimation.DecelerationRatio = 0.9f;  
      
                Storyboard.SetTarget(SlideAnimation, myImage);  
                Storyboard.SetTargetProperty(SlideAnimation, new PropertyPath("(Image.Margin)"));  
                sb.Children.Add(SlideAnimation);  
                sb.Begin();      
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful