2016-08-27 248 views
1

我正在創建一個WPF應用程序,並希望我的窗口無邊界,並且還可以調整大小只有頂部。如何從頂部調整無邊界窗口的大小?

我已經試過到目前爲止

  • 我最初以爲這會工作:

    <Window x:Class="WpfApplication3.MainWindow" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         Title="MainWindow" Width="200" Height="150" 
         WindowStyle="None" 
         ResizeMode="CanResize" 
         AllowsTransparency="True" 
         BorderThickness="0,5,0,0" 
         BorderBrush="Black"> 
        <Grid Background="Gray" /> 
    </Window> 
    

    得到只頂部邊框的窗口,但我無法調整它的大小。

    enter image description here

  • 然後我試圖WindowChrome.ResizeGripDirection="Top"ResizeMode="CanResizeWithGrip"

    <Window ... 
         WindowStyle="None" 
         ResizeMode="CanResizeWithGrip" 
         AllowsTransparency="True" 
         WindowChrome.ResizeGripDirection="Top" 
         BorderThickness="0,5,0,0" 
         BorderBrush="Black"> 
        ... 
    </Window> 
    

    這也不起作用(無法從頂部邊框調整大小),並且抓地力甚至不出現在頂部。它停留在右下角(我可以用調整的大小)。

    enter image description here

  • This answer好像回答者可以開始這樣做了,但代碼是不可用的。

  • This answer有一個鏈接到blog post,我不太想嘗試它,因爲我想要一個沒有代碼的解決方案。
  • 再有就是this answer

    • 我得到一個錯誤,用這種方法:

      <Window ... 
           WindowStyle="None" 
           ResizeMode="CanResizeWithGrip" 
           AllowsTransparency="False"> 
          <Grid Background="Gray" /> 
          <Setter Property="WindowChrome.WindowChrome"> 
           <Setter.Value> 
            <WindowChrome CornerRadius="0" 
                GlassFrameThickness="1" 
                UseAeroCaptionButtons="False"/> 
           </Setter.Value> 
          </Setter> 
      </Window> 
      

      屬性 '內容' 設置不止一次。

    • 隨着後面的代碼:

      <Window ... 
           WindowStyle="None" 
           ResizeMode="CanResize" 
           AllowsTransparency="False"> 
          <Grid Background="Gray" /> 
      </Window> 
      

      在構造函數中:

      WindowChrome chrome = new WindowChrome(); 
      chrome.CornerRadius = new CornerRadius(0); 
      chrome.GlassFrameThickness = new Thickness(0, 1, 0, 0); 
      chrome.UseAeroCaptionButtons = false; 
      

      這給了我:

      enter image description here

      而這可能會從所有的方向調整秒。而我只有希望它能夠從頂部調整大小。 (驚奇:我甚至沒有把任何東西都分配給新的chrome對象,這是如何工作的?這是我猜測的另一個問題)。


問題

我如何做一個無國界窗口可以上邊框被調整? (最好的做法是隻用可以更改顏色的頂部邊框。

回答

1

您可能已成功設置WindowChrome.ResizeBorderThickness屬性以刪除除頂部以外的所有邊框,例如, ResizeBorderThickness="0, 5, 0, 0"

它可能不是達到你的結果最乾淨的方式,但我已經有一些成功的適應這裏的答案:http://www.eidias.com/blog/2014/1/27/restyle-your-window(這是我找到致富WindowChrome工作的最簡單的方法):

創建自定義在ResourceDictionary窗口樣式:

<ResourceDictionary x:Class="WpfApplication.WindowStyle" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}"> 
     <Setter Property="WindowChrome.WindowChrome"> 
      <Setter.Value> 
       <WindowChrome CaptionHeight="30" 
           CornerRadius="4" 
           GlassFrameThickness="0" 
           ResizeBorderThickness="0, 5, 0, 0" 
           UseAeroCaptionButtons="False" /> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="Window.BorderThickness" Value="0, 5, 0, 0"/
    </Style> 
</ResourceDictionary> 

參考,其中所需要的字典(我把它放在App.xaml中):

<Application x:Class="WpfApplication1.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication1" 
     StartupUri="MainWindow.xaml"> 
    <Application.Resources> 
     <ResourceDictionary Source="WindowStyle.xaml" /> 
    </Application.Resources> 
</Application> 

參考在所需Window的風格:

<Window x:Class="WpfApplication1.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:WpfApplication1" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525" 
    Style="{StaticResource ResourceKey=CustomWindowStyle}"> 
    <Grid> 
    </Grid>  
</Window> 

這應該產生看起來像你的最後一個窗口,但只能從頂部調整(僅頂調整手柄,可以抓住)。