我的应用程序的某些元素具有自定义大小调整事件,这些事件都有效。然而,他们被一个案例搞砸了:
当悬停在窗口的边界上时,光标变成调整大小的 handle ,并且您单击(但不要拖动),元素调整大小不正确,并且我的处理程序没有被触发。
我一直在寻找这样的事件,但找不到匹配的任何内容。我只想为此事件创建一个处理程序,以避免调整元素大小时出现故障。
我正在使用 C#/WPF 和 .NET 4
窗口的 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">
事件处理器:
public void update_size(object sender, RoutedEventArgs e )
{
if (resultsTab.IsSelected){
Grid.SetRowSpan(dataGrid1, 2);
Grid.SetRowSpan(dataGrid2, 2);
}
}
此处理程序按预期执行,但在未拖动调整大小 handle 时不会触发,这让我相信单击调整大小 handle 是一个不同的事件。
此外,这只会发生一次 - 一旦我的调整大小处理程序生效,单击调整大小处理程序将无效。
最佳答案
将我的评论转化为答案:
这可以通过不在网格中放置任何 * 来解决。
此外,如果 Window 设置为 SizeToContent
,您应该只 SizeToContent=Width
以防止窗口无限缩放。
删除所有事件处理程序的东西,那是 hack。
关于c# - 窗口调整大小处理事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16922955/