Slower WPF Performance after a button gets focus

Dec 18 2008 8:18 AM
Hi all!
 
Could you please help me with my problem. I am writing an app that allows users to drag controls over a canvas and have met the following strange behaviour: wpf performance slowsdowns as soon as a button on a form receives focus. This is so strange to me. I've posted a code of a sample app, please try to click on a red rectangle, and move the mouse holding left button. You should see that rectangle follows the cursor pretty fast. But if you click (or even press Tab to give the button a focus) and try to drag the rectangle again - you will see that now rectangle is not as fast as before.. The recrangle will be still slow if you click Tab again and give focus to the textbox.
 
Do you know why it happens and how I can avoid it?

<Window x:Class="ButtonSpeedTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="627" Width="580">
    <Canvas Background="Wheat" MouseMove="MyMouseMove">
        <Button Canvas.Left="30" Canvas.Top="30" Height="37" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="66">Button</Button>
        <Rectangle MouseDown="MyMouseDown" MouseUp="MyMouseUp" MouseMove="MyMouseMove" Name="rect"  Canvas.Left="100" Canvas.Top="100" Fill="Red" Stroke="Black" Width="70" Height="50" />
        <TextBox Canvas.Left="246" Canvas.Top="37.52" Height="23" Name="textBox1" Width="120" />
    </Canvas>
</Window>

 

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ButtonSpeedTest
{
 public partial class Window1 : Window
 {
  bool isDragging;

  public Window1()
  {
   InitializeComponent();
  }

  private void MyMouseDown(object sender, MouseButtonEventArgs e)
  {
   isDragging = true;
  }

  private void MyMouseUp(object sender, MouseButtonEventArgs e)
  {
   isDragging = false;
  }  

  private void MyMouseMove(object sender, MouseEventArgs e)
  {
   if (isDragging)
   {
    Point position = e.GetPosition(this);

    rect.SetValue(Canvas.LeftProperty, position.X - rect.Width / 2);
    rect.SetValue(Canvas.TopProperty, position.Y - rect.Height / 4);
   }
  }
 }

Thanks,
Sergey.

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (3)