WPF:两个按钮的比例调整大小

时间:2016-04-16 14:30:16

标签: wpf xaml wpf-controls

我有一个带有两个按钮的WPF表单。当窗口调整大小时,每个按钮应始终占据窗口空间的一半
我无法让它发挥作用 怎么做?

<Window x:Class="ExampleWin.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:ExampleWin"
    mc:Ignorable="d"
    Title="Window" Height="200" Width="200" ToolTip="Tooltip" Topmost="True" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ResizeMode="CanResizeWithGrip">
<Border BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="5,5,5,5" UseLayoutRounding="True">
    <Grid>
        <Label x:Name="Backdrop" Content="Label" Margin="0,0,0,0" Foreground="{x:Null}" Background="#FFAD3838"/>
        <Button x:Name="Button1" Content="" Margin="1,1,99,1" BorderThickness="0" Background="#FF3B87BD"/>
        <Button x:Name="Button2" Content="" Margin="99,1,1,1" BorderThickness="0" Background="#FF59B483"/>
    </Grid>
</Border>

1 个答案:

答案 0 :(得分:2)

将ColumnDefinitions添加到Grid并使它们按比例填充可用空间(Width="*"这是默认值)

使用附加属性Grid.Column将Button1放入第1列(index = 0),将Button2放入第2列。如果您不需要按钮附近的空白区域,请更新保证金值

<Grid>
    <Grid.ColumnDefinitions>
         <ColumnDefinition/>
         <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Label x:Name="Backdrop" Grid.ColumnSpan="2" Content="Label" Margin="0,0,0,0" Foreground="{x:Null}" Background="#FFAD3838"/>
    <Button x:Name="Button1" Grid.Column="0" Content="" Margin="1" BorderThickness="0" Background="#FF3B87BD"/>
    <Button x:Name="Button2" Grid.Column="1" Content="" Margin="1" BorderThickness="0" Background="#FF59B483"/>
</Grid>