[英]Resize border NOT window in WPF
這是我的XAML:
<Window x:Class="IPCapture.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:IPCapture"
mc:Ignorable="d"
Title="IPCapture_GUI" Opacity="0.95" Background="Transparent" Foreground="White" FontFamily="Arial" FontSize="14" FontWeight="Bold" FontStretch="UltraExpanded" TextOptions.TextFormattingMode="Display" BorderThickness="0"
Loaded="Window_Loaded" WindowStartupLocation="CenterScreen" Height="Auto" Width="Auto" Cursor="Arrow" WindowStyle="None" AllowsTransparency="True" MouseLeftButtonDown="Window_MouseLeftButtonDown" Topmost="True" ResizeMode="CanResizeWithGrip">
<Window.OpacityMask>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="Black"/>
<GradientStop Color="#B2420000" Offset="1"/>
</LinearGradientBrush>
</Window.OpacityMask>
<Border CornerRadius="5" Background="#FFC20C0C" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Height="Auto">
<Grid x:Name="GridMain" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0">
<Grid.RowDefinitions>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="Keys" Width="Auto"/>
<ColumnDefinition x:Name="Values" Width="Auto"/>
</Grid.ColumnDefinitions>
</Grid>
<Border.Effect>
<DropShadowEffect Opacity="0.6" BlurRadius="1"/>
</Border.Effect>
</Border>
</Window>
我遵循了許多有關如何實現所需GUI的教程,終於成功了! 但是,它似乎沒有提供我需要的功能。 我想要:
我的問題的可視化:
藍色的大區域是我的Window
,左上角的小GUI是我的實際小部件。
(附在Border
)
從代碼中可以看到,我在Window
上設置了ResizeMode="CanResizeWithGrip"
。 我能夠完美調整窗口大小, 但調整其大小不會調整我的實際GUI。
(由於XAML的布局方式)
因此,由於這個原因,我想以某種方式允許調整邊框的寬度,而不是Window
。
補充:( 如果可能)
Window
頂部為中心的黑色小工具欄? Window
Height
和Width
設置為Auto
,它那么大嗎? 它不應該只擴大其內容的大小嗎? 讓我們一次來看一個:
調整大小的問題是由於您在Grid
和Border
上設置了HorizontalAlignment="Left" VerticalAlignment="Top"
。 這意味着元素將測量其所需的最小寬度和高度,並與左上方對齊。 如果刪除此元素,則元素(假設沒有其他布局問題)將調整大小以包含整個窗口。
將Width
和Height
設置為Auto
(順便說一下,這是默認設置)在窗口上的行為有所不同-它告訴OS-不是WPF-確定初始大小(使用未公開的算法,因此請不要依賴它)。 如果希望WPF將窗口調整為其內容所需的最小大小,請設置SizeToContent="WidthAndHeight"
。
黑色的小工具欄是VS 2015的新功能,僅在調試期間顯示。 您可以在VS的“實時視覺樹”面板中將其關閉。
獎勵提示:
我強烈建議您不要在整個視覺樹上應用Effect
。 這是巨大的性能損失。 您應該研究使用SystemDropShadowChrome
。
OpacityMask
。 除非確實需要漸變,否則就使用均勻的Opacity
...即使那樣,使用均勻的不透明度和漸變畫筆作為背景可能會更好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.