【问题标题】:How do I create a button that has a uniform width and height?如何创建具有统一宽度和高度的按钮?
【发布时间】:2011-07-21 04:07:36
【问题描述】:

我想创建一个按钮,其大小是其最大边缘的大小。

这样做的原因是我希望按钮是嵌入在DataGrid 列中的一个小信息圈。

目前我有:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Content="i" Padding="2" Margin="0"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    Width="{Binding Source=Self, Path=Height}">
                <Button.Template>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Ellipse Fill="#FFF4F4F5" Stroke="#FF6695EB"/>
                            <ContentPresenter Margin="0"
                                              RecognizesAccessKey="False"
                                              SnapsToDevicePixels="True"
                                              VerticalAlignment="Center"
                                              HorizontalAlignment="Center"
                                              />
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

【问题讨论】:

  • 如果您在 xaml 中对宽度或高度进行硬编码,您也可以对它们进行硬编码(并避免重写任何方法)。我在下面发布的解决方案不需要硬编码值,因为它覆盖了按钮的测量。
  • 谢谢 Rob,是的,我不想硬编码任何东西,在编辑后立即看到您的解决方案我决定将两个覆盖都添加到类中,这样它既适用于任何尺寸以及设置单一尺寸时。
  • 如果您希望能够选择性地硬编码宽度/高度,那么我建议将这些额外检查添加到 MeasureOverride 而不是使用 OnPropertyChanged
  • 谢谢 Rob,会做的,虽然对于我的主要用途我也不想指定,但您的解决方案可以正常工作:)

标签: wpf button styles


【解决方案1】:

创建一个派生自 Button 的类并覆盖测量,例如

public class SquareButton : Button
{
    protected override Size MeasureOverride(Size constraint)
    {
        Size size = base.MeasureOverride(constraint);
        size.Height = size.Width = Math.Max(size.Height, size.Width);
        return size;
    }
}

然后你可以像这样使用它:

<local:SquareButton 
    Content="i" Padding="2" Margin="0"
    VerticalAlignment="Center"
    HorizontalAlignment="Center">
    <local:SquareButton.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <Ellipse Fill="#FFF4F4F5" Stroke="#FF6695EB"/>
                <ContentPresenter Margin="0"
                    RecognizesAccessKey="False"
                    SnapsToDevicePixels="True"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    />
            </Grid>
        </ControlTemplate>
    </local:SquareButton.Template>
</local:SquareButton>

【讨论】:

    【解决方案2】:

    制作自定义控件 - SquareButton

    public class SquareButton : Button
    {
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (e.Property == HeightProperty || e.Property == WidthProperty)
                this.Height = this.Width = Math.Max(this.Width, this.Height);
    
            base.OnPropertyChanged(e);
        }
    }
    

    【讨论】:

    • 感谢 DerinDavis,但问题是它至少需要指定一个尺寸。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 2014-06-30
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2010-11-08
    相关资源
    最近更新 更多