Solved

Animating Text Property Values

Posted on 2010-09-21
2
493 Views
Last Modified: 2013-11-12
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
Comment
Question by:JSMcCloud
2 Comments
 
LVL 8

Accepted Solution

by:
adler77 earned 125 total points
Comment Utility
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
 

Author Closing Comment

by:JSMcCloud
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now