C#WPF - 当窗口调整大小时,如何使无边框窗口的内容跟随调整大小

时间:2015-04-27 04:17:39

标签: c# wpf

在我的wpf无边框窗口中,我有一个关闭按钮(图像)和一个选项卡控件。我可以通过设置窗口属性ResizeMode =" CanResizeWithGrip"来调整窗口的大小,但是窗口的内容(图像按钮和标签控件)没有。那么,我该如何克服这个问题呢?这是我的代码:

<Window x:Name="Window"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window" Height="240" Width="330" WindowStyle="None" AllowsTransparency="True" Background="{x:Null}" 
        Foreground="{x:Null}" ResizeMode="CanResizeWithGrip">
    <Border CornerRadius="5" BorderBrush="White" BorderThickness="2">
        <Grid>
            <Image x:Name="CloseBtn" HorizontalAlignment="Left" Height="19" Width="19" Margin="303,3,0,0" VerticalAlignment="Top" 
                   MouseLeftButtonDown="CloseBtn_MouseLeftButtonDown" ToolTip="Close">
                <Image.Style>
                    <Style TargetType="Image">
                        <Setter Property="Source" Value="Resources/closeButton.png"/>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Source" Value="Resources/closeHoverButton.png"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
            <Label x:Name="Title" Content="Title" HorizontalAlignment="Left" Margin="22,0,0,0" Width="200"
                   Height="22" Padding="3" VerticalAlignment="Top""/>
            <TabControl HorizontalAlignment="Left" Height="205" Width="310" Margin="8,25,0,0" VerticalAlignment="Top"  
                        FontFamily="Tahoma" FontSize="11">
                <TabItem Header="Header Title" FontFamily="Tahoma" FontSize="11">                    
                </TabItem>
            </TabControl>
        </Grid>
    </Border>
</Window>

1 个答案:

答案 0 :(得分:0)

因为您没有按照WPF布局方式布置控件的位置,所以它们将根据您的边距保持固定。

您应该阅读以下链接中的布局:http://www.wpftutorial.net/LayoutProperties.html

根据自己的喜好选择布局策略。

我的猜测是你刚刚使用设计师在表单上拖动了这些元素。当你这样做时,设计师一直在使用边距,并且实际上是在向错误的方向推进。

每当我在表单上拖动一个元素时,我首先要做的是删除边距。但在开始之前,我还会创建一个草图。想知道我需要多少行和列,或者我将使用哪种布局组合。因为是的,你可以将几种策略结合起来。