C# WPF:绑定后窗口大小更改事件

C# WPF:绑定后窗口大小更改事件,c#,wpf,events,xaml,C#,Wpf,Events,Xaml,各位程序员好 我用数据绑定的数据网格和其他东西制作了一个相当复杂的WPF应用程序。因为内容是动态变化的,所以窗口本身也会调整大小(正如它应该做的那样)。 我做了一个函数,当大小改变时,将窗口与主屏幕的中心对齐,如下所示: this.SizeChanged += delegate { double screenWidth = SystemParameters.PrimaryScreenWidth; double screenHeight = SystemParam

各位程序员好

我用数据绑定的数据网格和其他东西制作了一个相当复杂的WPF应用程序。因为内容是动态变化的,所以窗口本身也会调整大小(正如它应该做的那样)。 我做了一个函数,当大小改变时,将窗口与主屏幕的中心对齐,如下所示:

this.SizeChanged += delegate
   {
       double screenWidth = SystemParameters.PrimaryScreenWidth;
       double screenHeight = SystemParameters.PrimaryScreenHeight;
       double windowWidth = this.Width;
       double windowHeight = this.Height;
       this.Left = ( screenWidth / 2 ) - ( windowWidth / 2 );
       this.Top = ( screenHeight / 2 ) - ( windowHeight / 2 );
   };
就像我怀疑的那样。但是,由于内容是数据绑定的,因此大约需要1/4秒的时间才能获得内容。上面的SizeChanged事件在那个时候已经完成了它的工作,所以窗口根本没有居中

我是否可以在触发事件之前实现某种超时而不锁定所有内容


耐心等待您的回复

只是一个猜测,但其中一个可能有效

  this.SizeChanged += delegate
  {
      Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)delegate()
      {
          double screenWidth = SystemParameters.PrimaryScreenWidth;
          double screenHeight = SystemParameters.PrimaryScreenHeight;
          double windowWidth = this.Width;
          double windowHeight = this.Height;
          this.Left = (screenWidth / 2) - (windowWidth / 2);
          this.Top = (screenHeight / 2) - (windowHeight / 2);
      });
  };


  this.SizeChanged += delegate
  {
      ThreadPool.QueueUserWorkItem((o) =>
      {
          Thread.Sleep(100); //delay (ms)
          Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate()
          {
              double screenWidth = SystemParameters.PrimaryScreenWidth;
              double screenHeight = SystemParameters.PrimaryScreenHeight;
              double windowWidth = this.Width;
              double windowHeight = this.Height;
              this.Left = (screenWidth / 2) - (windowWidth / 2);
              this.Top = (screenHeight / 2) - (windowHeight / 2);
          });
      });
  };

就像我说的,只是一个猜测,因为我没有复制数据加载延迟的设置

  this.SizeChanged += delegate
  {
      Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)delegate()
      {
          double screenWidth = SystemParameters.PrimaryScreenWidth;
          double screenHeight = SystemParameters.PrimaryScreenHeight;
          double windowWidth = this.Width;
          double windowHeight = this.Height;
          this.Left = (screenWidth / 2) - (windowWidth / 2);
          this.Top = (screenHeight / 2) - (windowHeight / 2);
      });
  };


  this.SizeChanged += delegate
  {
      ThreadPool.QueueUserWorkItem((o) =>
      {
          Thread.Sleep(100); //delay (ms)
          Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate()
          {
              double screenWidth = SystemParameters.PrimaryScreenWidth;
              double screenHeight = SystemParameters.PrimaryScreenHeight;
              double windowWidth = this.Width;
              double windowHeight = this.Height;
              this.Left = (screenWidth / 2) - (windowWidth / 2);
              this.Top = (screenHeight / 2) - (windowHeight / 2);
          });
      });
  };

就像我刚才说的,只是一个猜测,因为我没有复制数据加载延迟的设置

第一个立即完成了工作!我会在调度器方面做更多的研究,因为它看起来非常有用。万分感谢!是的,如果你有一个繁忙的WPF应用程序,dispacher非常棒,BeginInvoke将调用async,但将是UI threadsafe:)-很高兴我能提供帮助。第一个立即完成了任务!我会在调度器方面做更多的研究,因为它看起来非常有用。万分感谢!是的,如果您有一个繁忙的WPF应用程序,Dispatcher非常棒,BeginInvoke将调用async,但将是UI线程安全的:)-很高兴我能提供帮助。