好吧,所以我现在已经在Google上搜索了几个小时,似乎找不到我所遇到的问题的直接答案。我有一个自定义窗口WindowStyle = "None"
和AllowsTransparency = "True"
当我点击我的最大化按钮时:
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
if(this.WindowState == WindowState.Normal)
{
App.Current.MainWindow.WindowState = WindowState.Maximized;
}
else
{
App.Current.MainWindow.WindowState = WindowState.Normal;
}
}
它几乎完全按照它的方式最大化,除非它看起来在窗口的顶部和左侧有-6px的边距。
我不希望那个空白区域存在(它只是白色,因为Google Chrome背后是开放的,它实际上是透明的)。我需要应用程序最大化以适应整个屏幕,不包括任务栏。到目前为止,我发现的唯一修复是在按下最大化按钮时将窗口边距设置为Margin = "6, 6, 0, 0"
。以下是其余代码供参考:
StartUp.xaml
<Window x:Class="Expense_Calculator.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:Expense_Calculator"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
AllowsTransparency="True">
<Grid Name="Container" Background="#323232">
<Grid.RowDefinitions>
<RowDefinition Height="33"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid>
<DockPanel Style="{StaticResource TitleDockPanel}">
<Label Style="{StaticResource TitleBarTitle}">App Name</Label>
<Button Name="CloseButton" Click="CloseButton_Click" DockPanel.Dock="Right" Style="{StaticResource TitleBarButtonClose}">
<Image Source="images/close.png"/>
</Button>
<Button Name="MaximizeButton" Click="MaximizeButton_Click" DockPanel.Dock="Right" Style="{StaticResource TitleBarButton}">
<Image Source="images/maximize.png"/>
</Button>
<Button Name="MinimizeButton" Click="MinimizeButton_Click" DockPanel.Dock="Right" Style="{StaticResource TitleBarButton}">
<Image Source="images/minimize.png"/>
</Button>
</DockPanel>
</Grid>
<Grid Style="{StaticResource UserArea}" Grid.Row="1">
<Grid Name="WelcomePage">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Style="{StaticResource Label1}">Welcome to your Expense Calculator!</Label>
<Button Cursor="Hand" Style="{StaticResource Button1}" Grid.Row="1">Get Started</Button>
</Grid>
</Grid>
</Grid>
StartUp.xaml.cs
using System.Windows;
namespace Expense_Calculator
{
/// <summary>
/// Interaction logic for StartUp.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
this.MaxHeight = SystemParameters.WorkArea.Height;
this.MaxWidth = SystemParameters.WorkArea.Width;
InitializeComponent();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
if(this.WindowState == WindowState.Normal)
{
App.Current.MainWindow.WindowState = WindowState.Maximized;
}
else
{
App.Current.MainWindow.WindowState = WindowState.Normal;
}
}
private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
}
}
的App.xaml
<Application x:Class="Expense_Calculator.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Expense_Calculator"
StartupUri="StartUp.xaml">
<Application.Resources>
<!--Title Bar-->
<Style x:Key="TitleDockPanel" TargetType="DockPanel">
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Background" Value="#323232"/>
<Setter Property="Height" Value="33"/>
</Style>
<Style x:Key="TitleBarTitle" TargetType="Label">
<Setter Property="Foreground" Value="White"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="FontWeight" Value="DemiBold"/>
<Setter Property="Padding" Value="10, 0"/>
</Style>
<Style x:Key="TitleBarButton" TargetType="Button">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="border" Background="#323232" Height="33" Width="33">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Width="15" Height="15"></ContentPresenter>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:.1"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="#464646" Duration="0"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="#3774FF" Duration="0"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="TitleBarButtonClose" TargetType="Button">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="border" Background="#323232" Height="33" Width="33">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Width="15" Height="15"></ContentPresenter>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:.1"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="Firebrick" Duration="0"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="#781414" Duration="0"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--End - Title Bar-->
<!--Welcome Page-->
<Style x:Key="UserArea" TargetType="Grid">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style x:Key="Label1" TargetType="Label">
<Setter Property="FontSize" Value="20"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Margin" Value="0, 0, 0, 25"/>
</Style>
<Style x:Key="Button1" TargetType="Button">
<Setter Property="Width" Value="Auto"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="border" Background="#323232" CornerRadius="16" BorderBrush="#505050" BorderThickness="1" Padding="15, 6">
<ContentPresenter x:Name="content" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.Foreground>
<SolidColorBrush Color="#7D7D7D"/>
</TextBlock.Foreground>
<TextBlock.FontSize>14</TextBlock.FontSize>
</ContentPresenter>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.15"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="#3C3C3C" Duration="0"/>
<ColorAnimation Storyboard.TargetName="content" Storyboard.TargetProperty="(TextBlock.Foreground).Color" To="White" Duration="0"/>
<ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="BorderBrush.Color" To="#C8C8C8" Duration="0"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Background.Color" To="#282828" Duration="0"/>
<ColorAnimation Storyboard.TargetName="content" Storyboard.TargetProperty="(TextBlock.Foreground).Color" To="White" Duration="0"/>
<ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="BorderBrush.Color" To="#C8C8C8" Duration="0"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Opacity" Value=".25"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--End - Welcome Page-->
</Application.Resources>
</Application>
答案 0 :(得分:5)
按照设计(出于什么原因,我不知道),当你有WindowStyle="None"
并且你最大化窗口时,它会在屏幕的实际边缘延伸几个像素。
在您的代码中,您将窗口的实际大小限制为工作区的确切尺寸。由于窗口的最大化仍然将窗口的左上角放在工作区域的左上角和左上角的几个像素上,窗口的可见部分必然小于工作的整个宽度。区域,因此右侧和底部的暴露区域。
As noted by commenter Evk,通过删除窗口上的大小限制(只有在窗口最大化时才能执行,如果您愿意),窗口可以扩展到WPF想要的完整大小,确保完全覆盖窗口工作区。
在您的后续评论中,目前尚不清楚您是否真的希望覆盖任务栏。在任何一种情况下,您可能会发现这些链接有助于满足您在这方面的特定需求:
Maximize window with WindowState Problem (application will hide windows taskbar)
Maximizing window (with WindowStyle=None) considering Taskbar
或者,您仍然可以设置大小限制,但考虑到WPF在窗口最大化时坚持的额外像素边距,将尺寸设置为大于所需尺寸,以便没有曝光区域。
对于它的价值,这里是一个简化的代码示例,仅关注此处的特定行为:
<Window x:Class="TestSO39578992MaximizeBorderless.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:p="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:TestSO39578992MaximizeBorderless"
mc:Ignorable="d" Background="Yellow"
WindowStyle="None" AllowsTransparency="True"
Title="MainWindow" Height="350" Width="525">
<Window.Style>
<p:Style TargetType="Window">
<Setter Property="WindowState" Value="Normal"/>
<!-- Uncomment "Topmost" setters to experiment with its effect on the task bar visibility -->
<!--<Setter Property="Topmost" Value="False"/>-->
<p:Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=checkBox1}" Value="True">
<Setter Property="WindowState" Value="Maximized"/>
<!--<Setter Property="Topmost" Value="True"/>-->
</DataTrigger>
</p:Style.Triggers>
</p:Style>
</Window.Style>
<!-- set the margin here, to account for the extra space WPF is adding -->
<!-- <Grid Margin="6"> -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<CheckBox x:Name="checkBox1" Content="Maximized" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBlock Text="Upper Right" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top"/>
<TextBlock Text="Lower Left" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
<TextBlock Text="Lower Right" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Column="1"/>
</Grid>
</Window>
当然:
public partial class MainWindow : Window
{
public MainWindow()
{
this.MaxHeight = SystemParameters.WorkArea.Height;
this.MaxWidth = SystemParameters.WorkArea.Width;
// Compensate for the extra space WPF adds by increasing the max width and height here
//this.MaxHeight = SystemParameters.WorkArea.Height + 12;
//this.MaxWidth = SystemParameters.WorkArea.Width + 12;
InitializeComponent();
}
}
我在所有四个角中都包含TextBlock
个元素,以便更容易看到窗口大小如何受各种属性值的影响。
答案 1 :(得分:1)
这是另一种简单的解决方法:
private void Window_StateChanged(object sender, EventArgs e)
{
if (this.WindowState == WindowState.Maximized)
{
this.BorderThickness = new Thickness(8);
}
else
{
this.BorderThickness = new Thickness(0);
}
}
答案 2 :(得分:0)
这样做,添加主视图的构造函数并在WindowState更改时创建一个事件。
private void UpdateMarginOnWindowState()
{
if (this.WindowState == WindowState.Maximized)
{
this.MainContainer.Margin = new Thickness(6);
return;
}
this.MainContainer.Margin = new Thickness(0);
}
答案 3 :(得分:0)
我遇到了同样的问题,但是通过使用
来调整窗口大小Me.ResizeMode = ResizeMode.NoResize
在将窗口状态更改为最大化后,消除了该问题
答案 4 :(得分:0)
检查后,我在窗口中增加了最大尺寸限制
MaxWidth="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}"
MaxHeight="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
然后,为了使触发的函数最大化,我按Glen Howard所述关闭了CanResize。
private void Maximize()
{
if (App.Current.MainWindow.WindowState == WindowState.Normal)
{
App.Current.MainWindow.ResizeMode = ResizeMode.NoResize;
MaximizeButton.Style = (Style)App.Current.Resources["RestoreButton"];
App.Current.MainWindow.WindowState = WindowState.Maximized;
}
else
{
App.Current.MainWindow.ResizeMode = ResizeMode.CanResize;
MaximizeButton.Style = (Style)App.Current.Resources["MaximizeButton"];
App.Current.MainWindow.WindowState = WindowState.Normal;
}
}
(Style)App.Current.Resources [“ RestoreButton”]更改我的按钮图标,如果您不想实现它,请不要查看它。