Reputation: 55
I am trying to get the ActualHeight of a TextBlock after it's text has been changed. This text may change multiple times on the same page, which means the Loaded method on my page, is only fired once..
Getting the value in the Page Loaded method works fine, but only for the first time. If i change the text of my textblock I am having trouble getting the new actualheight.
What I have tried so far:
TextBlock.Measure(new Size()); TextBlock.Arrange(new Rect());
to force the control to arrange and get the ActualHeight (Problem: This doesn't take into consideration if my textblock has been split into multiple lines either trough wrap or \n tags. Which means it will always return the value of a 1 line height textblock)I was thinking if there is a way to reload the Textblock
everytime I set it's value, so that I can get the actualheight through the TextBlock's Loaded method. But there doesn't seem to be a .Refresh() method for this in Windows Phone App's.
Does anyone have any idea's as to what I could do to get the actualheight including linebreaks, after I've set it's text to a new value?
Upvotes: 0
Views: 79
Reputation: 2593
Subscribe to the SizeChanged event. It's for precisely the use case you have presented.
MSDN Documentation - TextBlock Events, SizeChanged Event
That event will fire whenever the ActualHeight or ActualWidth properties change for the TextBlock element. As wpf controls inherit from the FrameworkElement base class, WPF controls all have access to this property.
Upvotes: 1