So I've read that ActualWidth
may equal 0 until it is fully loaded. I added its event handler, like so, to make sure it's fully loaded:
text.AddHandler(TextBlock.LoadedEvent, new RoutedEventHandler(textBlock_Loaded));
In the textBlock_Loaded
event, I have:
TextBlock tb = sender as TextBlock;
textBlockWidth = tb.ActualWidth;
I need to use the variable textBlockWidth
in my main method, but everytime I write the value of textBlockWidth to output in my Main method, I get 0.
So this question is, how do I ensure that ActualWidth
is NOT 0 before performing my actions? Since WPF is event-driven, is there a way I can trigger some method when it's done, instead of before? Otherwise, it'll return 0.
You won't be able to get the ActualWidth
. Since, it will not be updated at the time of loaded. So, you should use Dispatcher of the TextBlock
or it's Parent to get the ActualWidth
or ActualHeight
.
this.textBlock.Dispatcher.BeginInvoke((Action)(() =>
{
textBlock = this.textBlock.ActualWidth;
}));