WPF:绑定后的Window SizeChanged事件(WPF: Window SizeChanged event after binding)

编程入门 行业动态 更新时间:2024-10-24 15:18:44
WPF:绑定后的Window SizeChanged事件(WPF: Window SizeChanged event after binding)

程序员大家好,

我用数据绑定的数据网格和东西制作了一个相当复杂的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事件已经完成了它的工作,因此窗口根本不居中。

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

耐心等待你的回答!

Hello fellow programmers,

I've made a rather complicated WPF application with data bound datagrids and stuff. Since the content changes dynamically, the window itself resizes as well (as it's supposed to do). I made a function which aligns the window to the center of the primary screen when the size changes, like this:

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 ); };

It works like I suspected it to. However, since the content is data bound, it takes roughly 1/4th a second before the content is available. The SizeChanged event above already did it's job by that time, so the window is not centered at all.

Can i implement some sort of timeout before the event is triggered without locking everything up?

Awaiting your responses patiently!

最满意答案

只是一个猜测,但其中一个可能工作

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); }); }); };

就像我说的那样,因为我没有设置来复制数据加载延迟

Just a guess but one of these may work

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); }); }); };

Like I said just a guess as I dont have a setup to replicate the data load delay

更多推荐

本文发布于:2023-08-02 19:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   事件   Window   WPF   binding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!