[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 502
  • Last Modified:

Animating Text Property Values

I'm am developing a card game in which two cards battle each other and code the hit points agaisnt each other at the same time.

Even though the numbers are different, when they are finishing counting up to their maxium hit point against each other, I would like them to finish counting at the same time.

I'm using two text block controls, one for each card.  I had tried using DoubleAnimation to animate the value's text property, but it tells me that it's not of the right time, so I used DispatchTimer instead, but would like the option to use the story board as I can set their duration to be the same.

How can I fix this?
0
JSMcCloud
Asked:
JSMcCloud
1 Solution
 
adler77Commented:
If you add a Dependency Property to your UserControl, you can animate it. For example see below. In the places where I put [UserControlName] that means to put the class name of your UserControl. Now, you have a HitPoints property on your UserControl that you can animate and use DoubleAnimation because HitPoints is of type double. The property has a callback, which is just a method that gets called any time the value of the property changes. In this case, when the value changes, the TextBlock gets updated.
/// <summary>
/// Gets or sets the HitPoints
/// </summary>
public double HitPoints
{
    get { return (double)GetValue(HitPointsProperty); }
    set { SetValue(HitPointsProperty, value); }
}

/// <summary>
/// Identifies the HitPoints dependancy property
/// </summary>
public static readonly DependencyProperty HitPointsProperty =
    DependencyProperty.Register("HitPoints", typeof(double), typeof([UserControlName]),
    new PropertyMetadata(0D, new PropertyChangedCallback(OnHitPointsChanged)));

private static void OnHitPointsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    (([UserControlName])d).OnHitPointsChanged(e);
}

/// <summary>
/// Invokes the HitPoints property changed method.
/// </summary>
/// <param name="e">Event arguments.</param>
protected virtual void OnHitPointsChanged(DependencyPropertyChangedEventArgs e)
{
    HitPointsTextBlock.Text = HitPoints.ToString();
}

Open in new window

0
 
JSMcCloudAuthor Commented:
I code in VB so the C# code is a little hard for me to read, but even so, I understand what I need to do.  Basically, if I create a Dependency Property in my UserControl, it can then be animated and I can pass the value to the TextBlock's text property!  Makes since.  Thanks!!!!
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now