Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, I've done this in the past with the <code>ActualWidth</code> and <code>ActualHeight</code> properties, both of which are read-only. I created an attached behavior that has <code>ObservedWidth</code> and <code>ObservedHeight</code> attached properties. It also has an <code>Observe</code> property that is used to do the initial hook-up. Usage looks like this:</p> <pre class="lang-xml prettyprint-override"><code>&lt;UserControl ... SizeObserver.Observe="True" SizeObserver.ObservedWidth="{Binding Width, Mode=OneWayToSource}" SizeObserver.ObservedHeight="{Binding Height, Mode=OneWayToSource}" </code></pre> <p>So the view model has <code>Width</code> and <code>Height</code> properties that are always in sync with the <code>ObservedWidth</code> and <code>ObservedHeight</code> attached properties. The <code>Observe</code> property simply attaches to the <code>SizeChanged</code> event of the <code>FrameworkElement</code>. In the handle, it updates its <code>ObservedWidth</code> and <code>ObservedHeight</code> properties. Ergo, the <code>Width</code> and <code>Height</code> of the view model is always in sync with the <code>ActualWidth</code> and <code>ActualHeight</code> of the <code>UserControl</code>.</p> <p>Perhaps not the perfect solution (I agree - read-only DPs <em>should</em> support <code>OneWayToSource</code> bindings), but it works and it upholds the MVVM pattern. Obviously, the <code>ObservedWidth</code> and <code>ObservedHeight</code> DPs are <em>not</em> read-only.</p> <p>UPDATE: here's code that implements the functionality described above:</p> <pre class="lang-cs prettyprint-override"><code>public static class SizeObserver { public static readonly DependencyProperty ObserveProperty = DependencyProperty.RegisterAttached( "Observe", typeof(bool), typeof(SizeObserver), new FrameworkPropertyMetadata(OnObserveChanged)); public static readonly DependencyProperty ObservedWidthProperty = DependencyProperty.RegisterAttached( "ObservedWidth", typeof(double), typeof(SizeObserver)); public static readonly DependencyProperty ObservedHeightProperty = DependencyProperty.RegisterAttached( "ObservedHeight", typeof(double), typeof(SizeObserver)); public static bool GetObserve(FrameworkElement frameworkElement) { frameworkElement.AssertNotNull("frameworkElement"); return (bool)frameworkElement.GetValue(ObserveProperty); } public static void SetObserve(FrameworkElement frameworkElement, bool observe) { frameworkElement.AssertNotNull("frameworkElement"); frameworkElement.SetValue(ObserveProperty, observe); } public static double GetObservedWidth(FrameworkElement frameworkElement) { frameworkElement.AssertNotNull("frameworkElement"); return (double)frameworkElement.GetValue(ObservedWidthProperty); } public static void SetObservedWidth(FrameworkElement frameworkElement, double observedWidth) { frameworkElement.AssertNotNull("frameworkElement"); frameworkElement.SetValue(ObservedWidthProperty, observedWidth); } public static double GetObservedHeight(FrameworkElement frameworkElement) { frameworkElement.AssertNotNull("frameworkElement"); return (double)frameworkElement.GetValue(ObservedHeightProperty); } public static void SetObservedHeight(FrameworkElement frameworkElement, double observedHeight) { frameworkElement.AssertNotNull("frameworkElement"); frameworkElement.SetValue(ObservedHeightProperty, observedHeight); } private static void OnObserveChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { var frameworkElement = (FrameworkElement)dependencyObject; if ((bool)e.NewValue) { frameworkElement.SizeChanged += OnFrameworkElementSizeChanged; UpdateObservedSizesForFrameworkElement(frameworkElement); } else { frameworkElement.SizeChanged -= OnFrameworkElementSizeChanged; } } private static void OnFrameworkElementSizeChanged(object sender, SizeChangedEventArgs e) { UpdateObservedSizesForFrameworkElement((FrameworkElement)sender); } private static void UpdateObservedSizesForFrameworkElement(FrameworkElement frameworkElement) { // WPF 4.0 onwards frameworkElement.SetCurrentValue(ObservedWidthProperty, frameworkElement.ActualWidth); frameworkElement.SetCurrentValue(ObservedHeightProperty, frameworkElement.ActualHeight); // WPF 3.5 and prior ////SetObservedWidth(frameworkElement, frameworkElement.ActualWidth); ////SetObservedHeight(frameworkElement, frameworkElement.ActualHeight); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload