因此,我构建了此示例应用程序,并使用了带有WPF和C#的自定义WindowChrome。每次我尝试使用自己制作的按钮最大化窗口时,最大化窗口都会变得略微关闭吗?像是几个像素。发生什么事了?
<Window x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestApp"
mc:Ignorable="d"
Title="TestApp"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
MinHeight="500" MinWidth="940">
<WindowChrome.WindowChrome>
<WindowChrome
ResizeBorderThickness="6"
CaptionHeight="20"
GlassFrameThickness="0"
CornerRadius="0"
/>
</WindowChrome.WindowChrome>
<!-- Application -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="26" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- WindowChrone Header -->
<Grid Background="{StaticResource WindowChromeBrush}" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="RelayC" />
<Button Click="MinimizeWindow" Grid.Column="2" Style="{StaticResource WindowChromeButton}">
<Image Source="{StaticResource window_minimize_icon}"/>
</Button>
<Button Click="MaximizeWindow" Grid.Column="3" Style="{StaticResource WindowChromeButton}">
<Image Source="{StaticResource window_maximize_icon}" />
</Button>
<Button Click="CloseWindow" Grid.Column="4" Style="{StaticResource WindowChromeButton}">
<Image Source="{StaticResource window_close_icon}" />
</Button>
</Grid>
<!--- Main Content -->
<Grid Grid.Row="1">
</Grid>
</Grid>
</Window>
这是按钮功能的示例代码:
private void MinimizeWindow(object sender, RoutedEventArgs e) => this.WindowState = WindowState.Minimized;
private void MaximizeWindow(object sender, RoutedEventArgs e) => this.WindowState = WindowState.Maximized;
private void CloseWindow(object sender, RoutedEventArgs e) => this.Close();
我在做什么错了?
答案 0 :(得分:0)
我最终找到了解决该问题的一种优雅解决方案。我只是为最外面的网格分配了一个名称,只要最大化它就会被检测到,并添加一个边距以确保应用程序完全覆盖屏幕并且不会移开。然后,当屏幕尺寸更改时,我将删除所述边距。
这是我的一些代码:
主要功能:
private bool isMaximized = false;
private void CheckMaximizeChange(object sender, SizeChangedEventArgs e)
{
if (WindowState == WindowState.Maximized || (isMaximized && WindowState != WindowState.Minimized)) ChangeWindow();
}
private void MaximizeWindow(object sender, RoutedEventArgs e) => this.WindowState ^= WindowState.Maximized;
private void ChangeWindow()
{
isMaximized = !isMaximized;
MaximizeImageButton.Source = FindResource(MaximizeImageButton.Source == FindResource("window_maximize_icon") ? "window_restore_icon" : "window_maximize_icon") as DrawingImage;
MainGrid.Margin = new Thickness(MainGrid.Margin == new Thickness(0) ? 7 : 0);
}
然后在应用程序的构造函数中:
SizeChanged += CheckMaximizeChange;