如何使按钮大小为 window 的三分之一? WPF C#

How to make the button one third of the size of the window? WPF C#

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

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

感谢您的帮助:D

您可以将 Button 放入 star-sized Grid 中,以填充 window:

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

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

或者处理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;
    }
}