1

A DoubleAnimation changes a double value over time. I would like to set the Content property of a Label to this value. However, this fails because WPF does not convert the double to a string.

Do I have to subclass the Label and add a new double propery, which is bound to the Content, and let the DoubleAnimation change this new property? Or is there an easier way?

The double value in question is not limited to certain values, i.e. fixed StringKeyframes cannot be used.

Example use case: A countdown timer with extended precision, which displays the remaining time in a Label.

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
mafu
  • 31,798
  • 42
  • 154
  • 247
  • I'm sure this is a pretty stupid question, but I've not found an answer after half an hour of googling. – mafu Sep 01 '15 at 22:16
  • What do you mean "animate"? Just change the text displayed? Couldn't you do that by just binding "Content" to your property? – McGarnagle Sep 01 '15 at 22:32
  • @McGarnagle Yes, I could - but I would need to create this property first. I would like to avoid that. – mafu Sep 01 '15 at 22:33
  • 1
    @mafu Why do you want to avoid that? – 123 456 789 0 Sep 01 '15 at 23:07
  • @lll I feel it is too complicated for such a simple task. To my understanding, this would involve a) subclassing the Label b) adding a new double property ("NumericValue"), whose `set` would edit the Content and c) adding a NumericValueProperty (similar to `Label.OpacityProperty` etc.) so the animation can actually access it. I suppose 20 or 30 lines of code? – mafu Sep 02 '15 at 07:35
  • @mafu My 2 cents is that, if you are coming as a web developer you'll experience that in WPF some cases looks complicated for such a simple task and some cases you'll realize that WPF does more heavy lifting and makes it simple than doing it with HTML/JS. – 123 456 789 0 Sep 02 '15 at 17:02

1 Answers1

2

Set the Label ContentStringFormat="{}{0}", or use a TextBox and set StringFormat in the Text binding.

Mark Feldman
  • 15,731
  • 3
  • 31
  • 58
  • I've just tried setting ContentStringFormat but I'm having trouble getting it to work. I used: `lbl.ContentStringFormat = "{}{0}"; lbl.Content = "test"; var anim = new DoubleAnimation (10, 0, TimeSpan.FromSeconds (10)); lbl.BeginAnimation (Label.ContentProperty, anim);` This throws an AnimationException: Specified cast is not valid. Did I do something wrong? – mafu Sep 02 '15 at 07:48
  • Oh ok, I thought you were doing it in XAML. The others are correct, you'll need to create a Double dependency property or use one on an existing control. Incidentally why are you trying to do this in code-behind? This isn't WinForms, and honestly you're going to have a world of headache if you keep going down the path you are. What you should be doing is creating a view model and binding your view controls to that. – Mark Feldman Sep 02 '15 at 08:08
  • Ah, too bad. I've just tried to set everything in XAML instead of codebehind, but it failed with the same exception. Thank you for the hint, I'm still learning WPF and will read up on it. – mafu Sep 02 '15 at 08:12
  • 1
    No problem, good luck! [This article](https://msdn.microsoft.com/en-us/magazine/dd419663.aspx) will get you started, [this one](http://www.codeproject.com/Articles/278901/MVVM-Pattern-Made-Simple) is good too. – Mark Feldman Sep 02 '15 at 08:16