title: WPF: Binding to readonly property in code
body: <p>I'm currently doing some rescaling on data in a valueconverter whenever a panel is redrawn. I want to move some of this processing to the viewmodel as the most of the processing only occurs if the control size or a few other properties change.</p>
  
  <p>To ensure the rescaled data looks acceptable I need the <code>ActualWidth</code> of the container in the viewmodel. I want to bind it to a property of the viewmodel one way so when it changes I can trigger the rescaling processing.</p>
  
  <p>All the examples I could find bind a CLR or dependency property to an element rather than the other way and I'm clearly missing something in my understanding to work out how I should do it. I have have tried a few different things setting up the binding but am just not getting it right.</p>
  
  <p>Any hints? thanks.</p>
  
  <p>In MyView XAML:</p>
  
  <pre><code>&lt;myItemsControl/&gt;
  </code></pre>
  
  <p>In MyView code behind, something like:</p>
  
  <pre><code>Binding b = new Binding(MyWidthProperty);
  b.Mode = BindingMode.OneWay;
  b.Source = myItemsControl.Name;
  .........?
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public static readonly DependencyProperty MyWidthProperty = 
  DependencyProperty.Register( "MyWidth", typeof(Double), typeof(MyViewModel));
  </code></pre>
  
  <p>In MyViewModel:</p>
  
  <pre><code>    public Double MyWidth{
          get { return _myWidth; }
          set { _myWidth = value; ViewChanged(this); } }
  </code></pre>