As Ksiti Mehta mentioned, binding to ActualHeight and ActualWidth in WinRT is not reliable. But there is a good job where you do not need to use SizeChanged-Event:
Add this class:
public class ActualSizePropertyProxy : FrameworkElement, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public FrameworkElement Element { get { return (FrameworkElement)GetValue(ElementProperty); } set { SetValue(ElementProperty, value); } } public double ActualHeightValue { get { return Element == null ? 0 : Element.ActualHeight; } } public double ActualWidthValue { get { return Element == null ? 0 : Element.ActualWidth; } } public static readonly DependencyProperty ElementProperty = DependencyProperty.Register("Element", typeof(FrameworkElement), typeof(ActualSizePropertyProxy), new PropertyMetadata(null, OnElementPropertyChanged)); private static void OnElementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((ActualSizePropertyProxy)d).OnElementChanged(e); } private void OnElementChanged(DependencyPropertyChangedEventArgs e) { FrameworkElement oldElement = (FrameworkElement)e.OldValue; FrameworkElement newElement = (FrameworkElement)e.NewValue; newElement.SizeChanged += new SizeChangedEventHandler(Element_SizeChanged); if (oldElement != null) { oldElement.SizeChanged -= new SizeChangedEventHandler(Element_SizeChanged); } NotifyPropChange(); } private void Element_SizeChanged(object sender, SizeChangedEventArgs e) { NotifyPropChange(); } private void NotifyPropChange() { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ActualWidthValue")); PropertyChanged(this, new PropertyChangedEventArgs("ActualHeightValue")); } } }
Put it in resources:
<UserControl.Resources> <c:ActualSizePropertyProxy Element="{Binding ElementName=YourElement}" x:Name="proxy" /> </UserControl.Resources>
And bind its properties:
<TextBlock x:Name="tb1" Text="{Binding ActualWidthValue, ElementName=proxy}" />
source share