无法更改按钮高度 WPF

Can't change Button Height WPF

我有一个 UserControl,其中包含一个按钮 CollapseConsoleBtn:

<UserControl //namespaces
   <Grid
        Name="LoggingGrid"
        Height="100"
        Background="Black">
        <Grid.RowDefinitions>
            <RowDefinition Height="25" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <StackPanel
            Grid.Row="0"
            Margin="5,0,0,0"
            Orientation="Horizontal">
            <Button
                x:Name="CollapseBtn"
                Width="25"
                Height="25"
                HorizontalAlignment="Left"
                VerticalAlignment="Center"
                Click="CollapseBtn_Click"
                Content="▼"
                FontSize="12">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse Fill="White" />
                            <ContentPresenter
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Content="{TemplateBinding Content}" />
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Image
                Height="25"
                Margin="7,0,0,0"
                Source="/Images/console-icon.png"
                Visibility="Visible" />
            <Label
                Margin="2,0,0,0"
                Content="Console"
                FontSize="16"
                Foreground="White" />
        </StackPanel>
   </Grid>
</UserControl>

我的问题是我想让按钮变小——例如高度 20 和宽度 20。我可以改变宽度,但显然,高度固定为 25。即使我将它设置为15,大小不变。

有人遇到过这个问题吗?

我认为问题出在 <TextBlock Grid.Row="0" Margin="{StaticResource SmallLeftMargin}">(你把 Button 和 StackPanel 放在那里)。

我试图删除它,有点玩边距和填充,将按钮的大小设置为 16x16(MinWidth & MinHeight 属性)并得到这个结果:

用户控件XAML:

<Grid Name="LoggingGrid"
      Height="100"
      Background="Black">
    <Grid.RowDefinitions>
        <RowDefinition Height="26" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Button x:Name="CollapseButton"
            Click="CollapseBtn_Click"
            MinWidth="16"
            MinHeight="16"
            Margin="2,0,0,0"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            Grid.Row="0"
            Content="▼">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Ellipse Fill="White" />
                    <ContentPresenter HorizontalAlignment="Center"
                                      VerticalAlignment="Center"
                                      Content="{TemplateBinding Content}" />
                </Grid>
            </ControlTemplate>
        </Button.Template>
    </Button>
    <StackPanel Margin="5,0,0,0" 
                Orientation="Horizontal">
        <Image Height="25"
               Visibility="Visible" />
        <Label Margin="18,0,0,0"
               Content="Console"
               HorizontalContentAlignment="Center"
               VerticalContentAlignment="Center"
               Padding="0,0,0,2"
               FontSize="16"
               Foreground="White" />
    </StackPanel>
</Grid>

您可以做的是:

  1. 设置按钮的 MinHeight 而不是 Height
  2. 确保按钮的 HorizontalAlignmentVerticalAlignment 设置为默认值 Stretch