WPF:按比例调整两个按钮的大小 [英] WPF: proportional resize of two buttons

查看:28
本文介绍了WPF:按比例调整两个按钮的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 表单,里面有两个按钮.当窗口被调整大小时,每个按钮都应该占据窗口一半的空间.
我无法让它工作.
怎么做的?

I have a WPF form with two buttons in it. When the window is resized, each of the buttons should occupy half of the window's space at all times.
I can not get it to work.
How is it done?

<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>

推荐答案

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

add ColumnDefinitions to Grid and make them fill available space proportionally (Width="*" which is a default value)

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

put Button1 into 1st column (index=0) and Button2 into 2nd, using attached property Grid.Column. update Margin value if you don't need empty space near buttons

<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>

这篇关于WPF:按比例调整两个按钮的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆