Solved

Animating Text Property Values

Posted on 2010-09-21
2
498 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

733 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