title: WPF Custom Window Resize Issue
body: <p>I have exactly the same problem like this <a href="http://social.msdn.microsoft.com/Forums/vstudio/en-US/f984d743-6669-4192-8e0d-d586818e9349/wpf-custom-window-resizing?forum=wpf" rel="nofollow">one</a></p>
  
  <p>Everything is fine, but when i resize from <code>West to East</code> or <code>North to South</code>, the <code>East/South</code> side of the windows is kinda shaky, and it doesn't look very good.</p>
  
  <p>As i can see this post is from long, long time ago, and still there is no valid answer in it, i was looking around, but couldn't find anything, anyone can tell me if this issue is fixed and where i can find the solution?</p>
  
  <p><strong>[xaml]:</strong></p>
  
  <pre><code>&lt;ControlTemplate&gt;
      &lt;!-- MainGrid --&gt;
      &lt;Grid x:Name="PART_MainGrid"&gt;
          &lt;!-- MainGrid.Rows --&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" x:Name="PART_MainGridRow0" /&gt;
              &lt;RowDefinition Height="Auto" x:Name="PART_MainGridRow1" /&gt;
              &lt;RowDefinition Height="*" x:Name="PART_MainGridRow2" /&gt;
              &lt;RowDefinition Height="Auto" x:Name="PART_MainGridRow3" /&gt;
              &lt;RowDefinition Height="Auto" x:Name="PART_MainGridRow4" /&gt;
          &lt;/Grid.RowDefinitions&gt;
          &lt;!-- MainGrid.Columns --&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="Auto" x:Name="PART_MainGridCol0"/&gt;
              &lt;ColumnDefinition Width="Auto" x:Name="PART_MainGridCol1"/&gt;
              &lt;ColumnDefinition Width="*" x:Name="PART_MainGridCol2"/&gt;
              &lt;ColumnDefinition Width="Auto" x:Name="PART_MainGridCol3"/&gt;
              &lt;ColumnDefinition Width="Auto" x:Name="PART_MainGridCol4"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;!-- MainGrid.ShadowBorders --&gt;
          &lt;Border x:Name="PART_ShadowBorderWest"      Grid.Row="1" Grid.Column="0" Grid.RowSpan="3"/&gt;
          &lt;Border x:Name="PART_ShadowBorderNorth"     Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3"/&gt;            
          &lt;Border x:Name="PART_ShadowBorderEast"      Grid.Row="1" Grid.Column="4" Grid.RowSpan="3"/&gt;
          &lt;Border x:Name="PART_ShadowBorderSouth"     Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="3"/&gt;
          &lt;Border x:Name="PART_ShadowBorderNorthWest" Grid.Row="0" Grid.Column="0"/&gt;
          &lt;Border x:Name="PART_ShadowBorderNorthEast" Grid.Row="0" Grid.Column="4"/&gt;
          &lt;Border x:Name="PART_ShadowBorderSouthEast" Grid.Row="4" Grid.Column="4"/&gt;                
          &lt;Border x:Name="PART_ShadowBorderSouthWest" Grid.Row="4" Grid.Column="0"/&gt;
          &lt;!-- MainGrid.Borders --&gt;
          &lt;Border x:Name="PART_BorderWest"      Grid.Row="2" Grid.Column="1"/&gt;
          &lt;Border x:Name="PART_BorderNorth"     Grid.Row="1" Grid.Column="2"/&gt;
          &lt;Border x:Name="PART_BorderEast"      Grid.Row="2" Grid.Column="3"/&gt;
          &lt;Border x:Name="PART_BorderSouth"     Grid.Row="3" Grid.Column="2"/&gt;
          &lt;Border x:Name="PART_BorderNorthWest" Grid.Row="1" Grid.Column="1"/&gt;
          &lt;Border x:Name="PART_BorderNorthEast" Grid.Row="1" Grid.Column="3"/&gt;
          &lt;Border x:Name="PART_BorderSouthEast" Grid.Row="3" Grid.Column="3"&gt;                
          &lt;Border x:Name="PART_BorderSouthWest" Grid.Row="3" Grid.Column="1"/&gt;
      &lt;/Grid&gt;
  &lt;/ControlTemplate&gt;
  </code></pre>
  
  <p><strong>[c#]:</strong> </p>
  
  <pre><code>   // Global Variable
     private Point cursorOffset;
     #region All Resize Events and Functions
          public void AddNorthWestResizeEvents(FrameworkElement element)
          {
              if (element == null)
                  return;
  
              element.MouseEnter += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                      element.Cursor = Cursors.SizeNWSE;
                  else
                      element.Cursor = Cursors.Arrow;
              };
  
              element.MouseLeftButtonDown += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      cursorOffset.X = cursorLocation.X;
                      cursorOffset.Y = cursorLocation.Y;
  
                      element.CaptureMouse();
                  }
              };
  
              element.MouseMove += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; element.IsMouseCaptured &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      double nHorizontalChange = (cursorLocation.X - cursorOffset.X);
                      double nVerticalChange = (cursorLocation.Y - cursorOffset.Y);
  
                      if (Width - nHorizontalChange &gt; MinWidth)
                      {
                          Left += nHorizontalChange;
                          Width -= nHorizontalChange;
                      }
                      if (Height - nVerticalChange &gt; MinHeight)
                      {
                          Top += nVerticalChange;
                          Height -= nVerticalChange;
                      }
                  }
              };
  
              element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
          }
          public void AddNorthResizeEvents(FrameworkElement element)
          {
              if (element == null)
                  return;
  
              element.MouseEnter += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                      element.Cursor = Cursors.SizeNS;
                  else
                      element.Cursor = Cursors.Arrow;
              };
  
              element.MouseLeftButtonDown += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      cursorOffset.Y = cursorLocation.Y;
  
                      element.CaptureMouse();
                  }
              };
  
              element.MouseMove += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; element.IsMouseCaptured &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      double nHorizontalChange = (cursorLocation.X - cursorOffset.X);
                      double nVerticalChange = (cursorLocation.Y - cursorOffset.Y);
  
                      if (Height - nVerticalChange &gt; MinHeight)
                      {
                          Top += nVerticalChange;
                          Height -= nVerticalChange;
                      }
                  }
              };
  
              element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
          }
          public void AddNorthEastResizeEvents(FrameworkElement element)
          {
              if (element == null)
                  return;
  
              element.MouseEnter += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                      element.Cursor = Cursors.SizeNESW;
                  else
                      element.Cursor = Cursors.Arrow;
              };
  
              element.MouseLeftButtonDown += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      cursorOffset.X = (Width - cursorLocation.X);
                      cursorOffset.Y = cursorLocation.Y;
  
                      element.CaptureMouse();
                  }
              };
  
              element.MouseMove += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; element.IsMouseCaptured &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      double nHorizontalChange = (cursorLocation.X - cursorOffset.X);
                      double pHorizontalChange = (cursorLocation.X + cursorOffset.X);
                      double nVerticalChange = (cursorLocation.Y - cursorOffset.Y);
                      double pVerticalChange = (cursorLocation.Y + cursorOffset.Y);
  
                      if (pHorizontalChange &gt; MinWidth)
                          Width = pHorizontalChange;
                      if (Height - nVerticalChange &gt; MinHeight)
                      {
                          Top += nVerticalChange;
                          Height -= nVerticalChange;
                      }
                  }
              };
  
              element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
          }
          public void AddEastResizeEvents(FrameworkElement element)
          {
              if (element == null)
                  return;
  
              element.MouseEnter += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                      element.Cursor = Cursors.SizeWE;
                  else
                      element.Cursor = Cursors.Arrow;
              };
  
              element.MouseLeftButtonDown += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      cursorOffset.X = (Width - cursorLocation.X);
  
                      element.CaptureMouse();
                  }
              };
  
              element.MouseMove += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; element.IsMouseCaptured &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      double pHorizontalChange = (cursorLocation.X + cursorOffset.X);
  
                      if (pHorizontalChange &gt; MinWidth)
                          Width = pHorizontalChange;
                  }
              };
  
              element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
          }
          public void AddSouthEastResizeEvents(FrameworkElement element)
          {
              if (element == null)
                  return;
  
              element.MouseEnter += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                      element.Cursor = Cursors.SizeNWSE;
                  else
                      element.Cursor = Cursors.Arrow;
              };
  
              element.MouseLeftButtonDown += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      cursorOffset.X = (Width - cursorLocation.X);
                      cursorOffset.Y = (Height - cursorLocation.Y);
  
                      element.CaptureMouse();
                  }
              };
  
              element.MouseMove += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; element.IsMouseCaptured &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      double pHorizontalChange = (cursorLocation.X + cursorOffset.X);
                      double pVerticalChange = (cursorLocation.Y + cursorOffset.Y);
  
                      if (pHorizontalChange &gt; MinWidth)
                          Width = pHorizontalChange;
                      if (pVerticalChange &gt; MinHeight)
                          Height = pVerticalChange;
                  }
              };
  
              element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
          }
          public void AddSouthResizeEvents(FrameworkElement element)
          {
              if (element == null)
                  return;
  
              element.MouseEnter += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                      element.Cursor = Cursors.SizeNS;
                  else
                      element.Cursor = Cursors.Arrow;
              };
  
              element.MouseLeftButtonDown += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      cursorOffset.Y = (Height - cursorLocation.Y);
  
                      element.CaptureMouse();
                  }
              };
  
              element.MouseMove += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; element.IsMouseCaptured &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      double pVerticalChange = (cursorLocation.Y + cursorOffset.Y);
  
                      if (pVerticalChange &gt; MinHeight)
                          Height = pVerticalChange;
  
                  }
              };
  
              element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
          }
          public void AddSouthWestResizeEvents(FrameworkElement element)
          {
              if (element == null)
                  return;
  
              element.MouseEnter += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                      element.Cursor = Cursors.SizeNESW;
                  else
                      element.Cursor = Cursors.Arrow;
              };
  
              element.MouseLeftButtonDown += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      cursorOffset.X = cursorLocation.X;
                      cursorOffset.Y = (Height - cursorLocation.Y);
  
                      element.CaptureMouse();
                  }
              };
  
              element.MouseMove += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; element.IsMouseCaptured &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      double nHorizontalChange = (cursorLocation.X - cursorOffset.X);
                      double pVerticalChange = (cursorLocation.Y + cursorOffset.Y);
  
                      if (Width - nHorizontalChange &gt; MinWidth)
                      {
                          Left += nHorizontalChange;
                          Width -= nHorizontalChange;
                      }
                      if (pVerticalChange &gt; MinHeight)
                          Height = pVerticalChange;
                  }
              };
  
              element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
          }
          public void AddWestResizeEvents(FrameworkElement element)
          {
              if (element == null)
                  return;
  
              element.MouseEnter += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                      element.Cursor = Cursors.SizeWE;
                  else
                      element.Cursor = Cursors.Arrow;
              };
  
              element.PreviewMouseLeftButtonDown += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      cursorOffset.X = cursorLocation.X;
  
                      element.CaptureMouse();                    
                  }
              };
  
              element.MouseMove += (sender, e) =&gt;
              {
                  if (WindowState != WindowState.Maximized &amp;&amp; element.IsMouseCaptured &amp;&amp; ResizeMode == ResizeMode.CanResize)
                  {
                      Point cursorLocation = e.GetPosition(this);
  
                      double nHorizontalChange = (cursorLocation.X - cursorOffset.X);
  
                      if (Width - nHorizontalChange &gt; MinWidth)
                      {
                          Left += nHorizontalChange;
                          Width -= nHorizontalChange;
                      }
                  }
              };
  
              element.MouseLeftButtonUp += new MouseButtonEventHandler(ReleaseMouseCapture);
          }
  </code></pre>