【问题标题】:Can't change Button Height WPF无法更改按钮高度 WPF
【发布时间】:2021-12-01 01:54:21
【问题描述】:

我有一个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,它保持相同的大小。

有人遇到过这个问题吗?

【问题讨论】:

    标签: c# wpf button height


    【解决方案1】:

    我认为麻烦在于&lt;TextBlock Grid.Row="0" Margin="{StaticResource SmallLeftMargin}"&gt;(您将 Button 和 StackPanel 放在那里)。

    我尝试将其删除,稍微使用边距和填充,将按钮的大小设置为 16x16(MinWidthMinHeight 属性)并得到以下结果:

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

    【讨论】:

    • 我删除了TextBlock,甚至将按钮添加到StackPanel,并尝试了你所做的,但由于某种原因它对我不起作用。感谢您的努力!
    • 究竟是什么不起作用?按钮仍然没有改变它的大小?也许尝试更改对齐和边距或设置不是MinWidthMinHeight,而是隐式WidthHeight
    • 按钮不会改变它的大小,是的。就像我将按钮的 MinHeight 设置为等于 RowHeight 并且它只能更高,所以在我的情况下 >= 25。(我没有这样做,但它的行为就像我做了一样)这对我不起作用因为我希望按钮的大小在 20x20 左右。
    【解决方案2】:

    你可以做的是:

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

    【讨论】:

      猜你喜欢
      • 2016-01-23
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 2013-02-06
      • 1970-01-01
      • 2012-03-22
      • 1970-01-01
      相关资源
      最近更新 更多