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
Solved

Animating Text Property Values

Posted on 2010-09-21
2
497 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
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
Run Program using VBScript 3 73
Windows Drag & Drop Location 2 108
how to bring to back always mid parent image form 21 85
Problem to open text file 11 128
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 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