title: TwoWay binding on "linked" variables (Top/Left & Height/Width) with .NET 4.0
body: <p>I have encounted a problem when switching to the .NET Framework 4.0.
  I have a window that uses twoway binding on its Top/Left &amp; Widht/Height properties.</p>
  
  <p>A problem occurs when i need to change the viewModel.</p>
  
  <p>After changing the underlying ViewModel, when triggering the PropertyChanged event on the propertyname corresponding to my viewModel, the Left property's binding is triggered, moving the window to the correct X-location.
  But the action of <strong>moving the window triggers a "to source"</strong>, setting my viewModel's Top property.
  <strong>EDIT</strong> : There is not "set" done, but the Y Binding is not processed.</p>
  
  <p>Same behavior with the Height &amp; Width properties.</p>
  
  <p>Here is a tiny application that shows my problem.</p>
  
  <p>here is the model :</p>
  
  <pre><code> public class Model : INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
          public Position SelectedPos { get; set; }
          public Position Pos1 { get; set; }
          public Position Pos2 { get; set; }
  
      public Model( int x, int y, int x2, int y2 )
      {
          Pos1 = new Position( x, y );
          Pos2 = new Position( x2, y2 );
          SelectedPos = Pos1;
      }
  
      public void Toggle()
      {
          SelectedPos = Pos2;
  
          if( PropertyChanged != null )
          {
              var e = new PropertyChangedEventArgs( "SelectedPos" );
              PropertyChanged( this, e );
          }
      }
  }
  
  public class Position
  {
      int _x;
      public int X
      {
          get { return _x; }
          set { _x = value; }
      }
  
      int _y;
      public int Y
      {
          get { return _y; }
          set { _y = value; }
      }
  
      public Position( int x, int y )
      {
          X = x;
          Y = y;
      }
  }
  </code></pre>
  
  <p>And here is the view :</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525" 
          Left="{Binding Mode=TwoWay, Path=SelectedPos.X}"
          Top="{Binding Mode=TwoWay, Path=SelectedPos.Y}"&gt;
      &lt;Grid&gt;
          &lt;Button Click="Button_Click"&gt;Change Position&lt;/Button&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And finally the code-behind :</p>
  
  <pre><code>namespace WpfApplication1
  {
      public partial class MainWindow : Window
      {
          Model model;
  
          public MainWindow()
          {
              InitializeComponent();
              model = new Model( 5, 5, 500, 500 );
              DataContext = model;
          }
  
          private void Button_Click( object sender, RoutedEventArgs e )
          {
              model.Toggle();
          }
      }
  }
  </code></pre>
  
  <p>What I would've liked to know is if there is some kind a way to "freeze" a binding, to prevent the engine from setting my viewModel until it has finished processing all the binding I asked it to do. or to switch the binding from twoWay to OneWay, for a short period of time.</p>
  
  <p>The small application here behaves correctly when using the .NET framework 3.5 but doesn't 
  with the 4.0.</p>
  
  <p>I am surprised I couldn't find anyone struggling  with the same issue, am i doing something wrong ?
  I thank you for your answers, don't hesitate to ask if something is not clear.</p>
  
  <p>Jean-Loup Kahloun</p>
  
  <hr>
  
  <p>I've added logs this morning (i should've done that before posting here..), just like you did, and you are right, there is no "set" done, but the Y binding is not processed.
  The behavior is even more strange when actually toggling several times the position of the window.</p>
  
  <p>I'm going to test your solution, even though I would have liked to avoid using code behind ( I used some for test purposes only ).</p>
  
  <p>I thank you for answering so fast, i'll get back at you when I have some time to find a code-behind-free solution. 
  As the model is not set, i may be able to trigger seperately the X and Y bindings with propertychanged events, right after the weird-behaving "Position" binding is triggered.</p>
  
  <p>Thanks again, i was going the wrong way, you saved me loads of time.</p>