Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Situation: I set Storyboard.SetTargetProperty for BlurEffect by string.
Problem: It feels not easy, because IntelliSense doesn't work for string.
Workaround: I feel i can do it by in a complex way, but the goal is to do, for example, just like i do with Line position setting it by reference.
Questions:
1. How to set it by reference easy without complexity?
2. How can i know what property path to use?
3. How to enable IntelliSense?
Notes:
1. It must be done in C# using Storyboard, not XAML.
2. Setting by reference works fine, for example, for Line position, but the same doesn't work for BlurEffect at all.
Code:
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.Loaded += MainWindow_Loaded;
    }

    Line line;
    BlurEffect blur;

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Canvas canvas = new Canvas();
        canvas.Background = Brushes.Black;
        this.Content = canvas;

        blur = new BlurEffect();
        blur.Radius = 20;

        line = new Line();
        line.Stroke = Brushes.White;
        line.X1 = 0;
        line.Y1 = 0;
        line.X2 = 100;
        line.Y2 = 100;
        line.Effect = blur;

        canvas.Children.Add(line);

        Animation();
    }

    private void Animation()
    {
        DoubleAnimation da = new DoubleAnimation();
        da.To = 0;
        da.Duration = TimeSpan.FromSeconds(1);

        Storyboard sb = new Storyboard();
        Storyboard.SetTarget(da, line);
        Storyboard.SetTargetProperty(da, new PropertyPath("Effect.Radius")); // How to set by reference?
        sb.Children.Add(da);
        sb.Begin();
    }
}


> as any dependency property that is of double format is valid for animation.
The next code doesn't work:
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.Loaded += MainWindow_Loaded;
    }

    BlurEffect blur;

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Canvas canvas = new Canvas();
        canvas.Background = Brushes.Black;
        this.Content = canvas;

        blur = new BlurEffect();
        blur.Radius = 20;

        Line line = new Line();
        line.Stroke = Brushes.White;
        line.X1 = 0;
        line.Y1 = 0;
        line.X2 = 100;
        line.Y2 = 100;
        line.Effect = blur;

        canvas.Children.Add(line);

        Animation();
    }

    private void Animation()
    {
        DoubleAnimation da = new DoubleAnimation();
        da.To = 0;
        da.Duration = TimeSpan.FromSeconds(0);

        Storyboard sb = new Storyboard();
        Storyboard.SetTarget(da, blur);
        Storyboard.SetTargetProperty(da, new PropertyPath(BlurEffect.RadiusProperty));
        sb.Children.Add(da);
        sb.Begin(); // Doesn't animate.
    }
}

But when i call BeginAnimation on a BlurEffect instance, it works.
Code:
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.Loaded += MainWindow_Loaded;
    }

    BlurEffect blur;

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Canvas canvas = new Canvas();
        canvas.Background = Brushes.Black;
        this.Content = canvas;

        blur = new BlurEffect();
        blur.Radius = 20;

        Line line = new Line();
        line.Stroke = Brushes.White;
        line.X1 = 0;
        line.Y1 = 0;
        line.X2 = 100;
        line.Y2 = 100;
        line.Effect = blur;

        canvas.Children.Add(line);

        Animation();
    }

    private void Animation()
    {
        DoubleAnimation da = new DoubleAnimation();
        da.To = 0;
        da.Duration = TimeSpan.FromSeconds(0);
        blur.BeginAnimation(BlurEffect.RadiusProperty, da); // Works.
    }
}
Posted
Updated 8-Sep-15 1:54am
v14
Comments
Kenneth Haugland 8-Sep-15 6:23am    
I guess you can do this:
https://www.visualstudio.com/en-us/integrate/explore/explore-vside-vsi.aspx

Perhaps not the easiest solution but .... :laugh:
Ziya1995 8-Sep-15 6:54am    
> Perhaps not the easiest solution but .... :laugh:
No, it is not what i want.
Turns out it is impossible without extensions or 3rd party tools.

So how can i know what property path to use?
For example, how could i know in this case that i should write "Effect.Radius"?
How can i get a list of property paths?
Is there a place on msdn describing all of them?
Kenneth Haugland 8-Sep-15 7:15am    
I think you are asking the wrong questions here, as any dependency property that is of double format is valid for animation. You would simply have to check the animation type with the property that you want to animate, and perhaps also its to and/or from values are withing the boundaries of the property you want to animate.
Ziya1995 8-Sep-15 7:27am    
> as any dependency property that is of double format is valid for animation.
Not, it is not true and that is a problem.
BlurEffect.RadiusProperty is not valid for animation.
I can animate Line.X2Property, but not BlurEffect.RadiusProperty in the same way using Storyboard.
If you can animate BlurEffect.RadiusProperty using Storyboard, you can post the code.

The question remains:
How can i know about property paths?
Kenneth Haugland 8-Sep-15 7:33am    
Not true:
http://bursjootech.blogspot.de/2008/06/grayscale-effect-pixel-shader-effect-in.html

All PixelSHader Effects deriving from the ShaderEffects class are dependencyproperties

This works:
VB
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Effects

Class MainWindow

    Dim blur As New BlurEffect
    Dim line As New Line()
    Dim myStoryboard As New Storyboard()

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
        Dim canvas As New Canvas()
        canvas.Background = Brushes.Black
        Me.Content = canvas

        blur = New BlurEffect()
        blur.Radius = 1

        line.Stroke = Brushes.White
        line.X1 = 0
        line.Y1 = 0
        line.X2 = 100
        line.Y2 = 100
        line.Effect = blur

        canvas.Children.Add(line)

        Animation()
    End Sub

    Private Sub Animation()
        ' NameScope.SetNameScope(Me, New NameScope())

        ' Create BitmapEffects that will be animated.
        Dim myBlurBitmapEffect As New BlurEffect()

        ' Assign the BlurBitmapEffect a name so that
        ' it can be targeted by a Storyboard.
        Me.RegisterName("myBlurBitmapEffect", myBlurBitmapEffect)

        myBlurBitmapEffect.Radius = 0


        line.Effect = myBlurBitmapEffect

        Dim da As New DoubleAnimation()
        da.To = 20
        da.Duration = TimeSpan.FromSeconds(5)

        ' Set the animation to target the Radius property
        ' of the object named "myBlurBitmapEffect."
        Storyboard.SetTargetName(da, "myBlurBitmapEffect")
        Storyboard.SetTargetProperty(da, New PropertyPath(BlurEffect.RadiusProperty))

        myStoryboard.Children.Add(da)

    End Sub

    Private Sub Window_MouseDown(sender As Object, e As MouseButtonEventArgs)
        myStoryboard.Begin(Me)
    End Sub
End Class
 
Share this answer
 
Comments
Ziya1995 8-Sep-15 8:26am    
I tried it in C# and it works.
RegisterName solved the problem.
Code (C#):
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.Loaded += MainWindow_Loaded;
    }

    BlurEffect blur;

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Canvas canvas = new Canvas();
        canvas.Background = Brushes.Black;
        this.Content = canvas;

        blur = new BlurEffect();
        blur.Radius = 20;

        Line line = new Line();
        line.Stroke = Brushes.White;
        line.X1 = 0;
        line.Y1 = 0;
        line.X2 = 100;
        line.Y2 = 100;
        line.Effect = blur;

        canvas.Children.Add(line);

        Animation();
    }

    private void Animation()
    {
        DoubleAnimation da = new DoubleAnimation();
        da.To = 0;
        da.Duration = TimeSpan.FromSeconds(0);

        string name = "blur";
        RegisterName(name, blur);

        Storyboard sb = new Storyboard();
        Storyboard.SetTargetName(da, name);
        Storyboard.SetTargetProperty(da, new PropertyPath(BlurEffect.RadiusProperty));
        sb.Children.Add(da);
        sb.Begin(this); // Animates.
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900