wpf 窗口调整大小处理事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16922955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Window Resize Handle Event
提问by user1663203
Certain elements of my application have custom resizing events, which all work. However, they are messed up by one case:
我的应用程序的某些元素具有自定义调整大小事件,这些事件都可以正常工作。然而,他们被一个案例搞砸了:
When hovering over the border of the window, so that the cursor becomes the resize handle, and you click (but do not drag), the elements resize incorrectly, and my handlers are not fired.
当鼠标悬停在窗口边框上时,使光标成为调整大小的手柄,然后单击(但不要拖动),元素会错误地调整大小,并且不会触发我的处理程序。
I've looked for such an event but cannot find anything that matches. I'd like to simply make a handler for this event to avoid glitchy resizing of my elements.
我一直在寻找这样的事件,但找不到任何匹配的事件。我想简单地为这个事件创建一个处理程序,以避免我的元素调整大小出现故障。
I'm using C#/WPF, with .NET 4
我正在使用 C#/WPF 和 .NET 4
xaml for the window:
窗口的 xaml:
<Window x:Class="XHealth.MainWindow"
Name="mainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataGridTemplateSample"
xmlns:XH="clr-namespace:XHealth"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
SizeChanged="update_size"
Title="XHealth" Loaded="Window_Loaded" WindowState="Normal" WindowStartupLocation="CenterScreen" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" SizeToContent="WidthAndHeight" WindowStyle="ThreeDBorderWindow" MinWidth="650" MinHeight="648" Width="Auto" VerticalAlignment="Top" DataContext="{Binding}" PreviewKeyDown="Window_KeyDown">
Event handler:
事件处理程序:
public void update_size(object sender, RoutedEventArgs e )
{
if (resultsTab.IsSelected){
Grid.SetRowSpan(dataGrid1, 2);
Grid.SetRowSpan(dataGrid2, 2);
}
}
This handler performs as intended, but does not trigger when the resize handle is not dragged, which leads me to believe clicking the resize handle is a different event.
此处理程序按预期执行,但在未拖动调整大小手柄时不会触发,这使我相信单击调整大小手柄是一个不同的事件。
Also, this only happens once - once my resize handler takes effect, clicked the resize handle has no effect.
此外,这只会发生一次 - 一旦我的调整大小处理程序生效,单击调整大小处理程序无效。
采纳答案by Federico Berasategui
Converting my comment into an answer:
将我的评论转换为答案:
That could be resolved by not putting any * in the Grid.
这可以通过不在网格中放置任何 * 来解决。
Also, if the Window is set to SizeToContent, you should only SizeToContent=Widthto prevent the window from scaling endlessly.
此外,如果 Window 设置为SizeToContent,您应该只SizeToContent=Width防止窗口无休止地缩放。
Remove all the event handlers stuff, that's hack.
删除所有事件处理程序的东西,那是黑客。

