简体   繁体   English

无法更改按钮高度 WPF

[英]Can't change Button Height WPF

I have a UserControl which contains a button CollapseConsoleBtn :我有一个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>

在此处输入图片说明

My problem here is that I want to make the button smaller - for example with Height 20 and Width 20. I can change the width, but apparently, the height is fixed to be 25. Even if I set it to 15, it remains the same size.我的问题是我想让按钮变小 - 例如使用高度 20 和宽度 20。我可以更改宽度,但显然,高度固定为 25。即使我将其设置为 15,它仍然是相同的大小。

Has anyone encountered this problem?有没有人遇到过这个问题?

I think trouble is in <TextBlock Grid.Row="0" Margin="{StaticResource SmallLeftMargin}"> (you put Button and StackPanel there).我认为问题出在<TextBlock Grid.Row="0" Margin="{StaticResource SmallLeftMargin}"> (你把 Button 和 StackPanel 放在那里)。

I tried to removed it, a bit played with margins and paddings, setted a size of button 16x16 ( MinWidth & MinHeight properties) and get this result:我试图删除它,有点使用边距和填充,设置按钮 16x16 的大小( MinWidthMinHeight属性)并得到以下结果:

在此处输入图片说明

UserControl XAML:用户控件 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>

What you can do is:你可以做的是:

  1. Set the button's MinHeight instead of Height .设置按钮的MinHeight而不是Height
  2. Make sure that the button's HorizontalAlignment and VerticalAlignment are set as their default value is Stretch .确保按钮的HorizontalAlignmentVerticalAlignment设置为它们的默认值是Stretch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM