2016-09-19 58 views
3

好的,所以我一直在搜索谷歌幾個小時,似乎無法找到我遇到的問題的直接答案。我有WindowStyle = "None"AllowsTransparency = "True"自定義窗口當我在我的最大化按鈕點擊:WPF - 無界限窗口無法正確最大化

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保證金。

Here's what it looks like

我不想說白了空間,在那裏(這是唯一的白人,因爲谷歌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

嘗試刪除this.MaxHeight = SystemParameters.WorkArea.Height和this.MaxWidth = SystemParameters.WorkArea.Width;來自構造函數。 – Evk

+0

當我這樣做時,應用程序最大化以適應整個屏幕。所以它涵蓋了任務欄。它雖然擺脫了所有的空白空間。但它也似乎重疊的雙方,你可以看到[這裏](http://imgur.com/1HDswCV) –

回答

4

通過設計(出於何種原因,我不知道),當您有WindowStyle="None"並且您最大化窗口時,它將延伸到屏幕的實際邊緣以外的所有邊上幾個像素。

在您的代碼中,您將窗口的實際大小限制爲工作區域的確切尺寸。由於窗口的最大化仍將窗口的左上角放置在工作區域的左上角和左上角的幾個像素上,窗口的可見部分必然小於工作的整個寬度區域,因此右側和底部的暴露區域。通過刪除窗口上的大小限制(只有當窗口最大化時,如果你喜歡,你可以做到這一點),窗口可以擴展到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元素四個角,以便更容易地瞭解窗口大小如何受各種屬性值的影響。

+0

我真的不理解你的[第二鏈接](https://blogs.msdn.microsoft.com/llobo/2006/08/01 /最大化窗口與 - windowstylenone-考慮,任務欄/)。主要是因爲我不確定如何使用它。但是當我使用你的代碼時,它仍然是一樣的結果。每當我使用'this.MaxHeight = SystemParameters.WorkArea.Height + 6;'(並且寬度相同),它覆蓋整個屏幕。但工作領域似乎並不重要。頂部和左側仍然被切斷。它似乎是工作區域是6px並且離開。另外我試圖不掩蓋任務欄。 –

+0

要清楚:我上面的代碼不是解決方案,它只是一種重現問題的方法,不需要您原始示例顯示的所有額外內容。請注意,除了使窗口更大(通過在設置最大高度和寬度時添加補償),還需要爲窗口內容元素設置「邊距」,以便窗口內容佈局本身也佔額外利潤率WPF正在增加。我將使用註釋行編輯代碼示例以顯示我的意思。 –

+0

好吧,我錯了,我誤解了你的答案。我很感謝你的詳細回答。這確實解決了問題,所以我要接受你的答案。感謝) –