【问题标题】:How to make the button one third of the size of the window? WPF C#如何使按钮大小为窗口大小的三分之一? WPF C#
【发布时间】:2021-11-09 19:24:26
【问题描述】:

我想以百分比指定按钮的宽度。 WPF 中是否有类似 width: 33% 的东西? 这是我的按钮:

<Button x:Name="btnWebsite" Content="Button" HorizontalAlignment="Left" Margin="58,342,0,0" VerticalAlignment="Top" Height="43" Width="139"/>

感谢您的帮助:D

【问题讨论】:

  • 我认为您的问题中没有出现某些内容(图像、代码)。
  • 您可以获取表单的宽度并使用它修改按钮的宽度。使用.ActualHeight.ActualWidth 获取实际值。

标签: c# wpf button size


【解决方案1】:

您可以将Button 放入填充窗口的star-sized Grid

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

        <Button Content="..." Grid.Column="1" />
    </Grid>
</Window>

或者处理窗口的SizeChanged事件,设置按钮的Width属性为this.Width / 3.0

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        SizeChanged += OnSizeChanged;
    }

    private void OnSizeChanged(object sender, SizeChangedEventArgs e)
    {
        btnWebsite.Width = Width / 3.0;
    }
}

【讨论】:

  • 非常感谢,非常感谢!
  • 当我使用最大化按钮最大化窗口时,按钮没有调整。我该如何更改?
  • 它应该使用我的示例标记进行调整,即如果Grid 是最大化窗口的Content
猜你喜欢
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
  • 2011-07-14
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多