Solved

Animating Text Property Values

Posted on 2010-09-21
2
499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
adler77 earned 125 total points
ID: 33730265
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
ID: 33730390
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
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 is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

695 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