且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使按钮内的网格在 WPF 中具有 100% 的宽度?

更新时间:2023-10-19 20:19:52

你必须直接在 Button 上设置 Horizo​​ntalContentAlignmentStrech像这样:

You have to set the HorizontalContentAlignment to Strech on the Button directly like so:

<Window x:Class="GridInButtonIssue.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:GridInButtonIssue"
        mc:Ignorable="d"
        Title="MainWindow" Height="136.5" Width="269.839">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Button x:Name="btn_SelectMode" Grid.Row="0" Margin="0,35,0,0" HorizontalContentAlignment="Stretch" >
      <Grid HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
          <RowDefinition Height="20" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="6*" />
          <ColumnDefinition Width="4*" />
        </Grid.ColumnDefinitions>
        <Canvas x:Name="svg2" Grid.Column="0" Width="25" Height="25" HorizontalAlignment="Center">
          <Canvas.RenderTransform>
            <TranslateTransform X="0" Y="0"/>
          </Canvas.RenderTransform>
          <Canvas x:Name="layer1">
            <Canvas.RenderTransform>
              <TranslateTransform X="-298.50531" Y="-576.33075"/>
            </Canvas.RenderTransform>
            <Ellipse xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Canvas.Left="298.6" Canvas.Top="576.4" Width="19.9" Height="19.9" x:Name="path4144" Fill="#FF951718" StrokeThickness="0.14452878" Stroke="#FFFD0000" StrokeMiterLimit="4" StrokeLineJoin="Miter" StrokeStartLineCap="Flat" StrokeEndLineCap="Flat" Opacity="1"/>
          </Canvas>
        </Canvas>
        <TextBlock Grid.Column="1" Text="Test Button" HorizontalAlignment="Left" />
      </Grid>
    </Button>
  </Grid>
</Window> 

所以它看起来像这样: