WPF 动态布局:如何强制平方比例(宽度等于高度)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2981705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:50:33  来源:igfitidea点击:

WPF dynamic layout: how to enforce square proportions (width equals height)?

wpfxaml

提问by Gart

I'm learning WPF and can't figure out how to enfore my buttons to take a square shape.

我正在学习 WPF,但不知道如何使我的按钮呈方形。

Here is my XAML Markup:

这是我的 XAML 标记:

<Window x:Class="Example"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="368" Width="333">
  <Window.Resources>
    <Style x:Key="ToggleStyle" BasedOn="{StaticResource {x:Type ToggleButton}}"
                            TargetType="{x:Type RadioButton}">
    </Style>
  </Window.Resources>
  <RadioButton Style="{StaticResource ToggleStyle}">
        Very very long text
  </RadioButton>
</Window>

Specifying explicit values for Widthand Heightattributes seems like a wrong idea - the button should calculate its dimensions based on its contents automagically, but keep its width and height equal. Is this possible?

WidthHeight属性指定显式值似乎是一个错误的想法 - 按钮应该根据其内容自动计算其尺寸,但保持其宽度和高度相等。这可能吗?

回答by Dan Puzey

Try this - it seems to work in Kaxaml:

试试这个 - 它似乎在 Kaxaml 中工作:

<Button 
    MinWidth="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" 
    MinHeight="{Binding ActualWidth, RelativeSource={RelativeSource Self}}">
  Some content
</Button>

(To test, I put a TextBox inside the button, because that's an easy way to change content size without re-parsing the Xaml.)

(为了测试,我在按钮内放置了一个 TextBox,因为这是一种无需重新解析 Xaml 即可更改内容大小的简单方法。)

Edit:sorry, should probably have specified it as a style to match your example:

编辑:抱歉,可能应该将其指定为与您的示例相匹配的样式:

<Style TargetType="Button" x:Key="SquareButton">
  <Setter Property="MinWidth" Value="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" />
  <Setter Property="MinHeight" Value="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" />
</Style>

回答by n535

I think you want to bind button's width to its height, like this:

我认为您想将按钮的宽度绑定到其高度,如下所示:

<Button Name="myButton"
Width="{Binding ElementName=myButton, Path=Height}"
Height="100">
Button Text
</Button>

回答by yvan vander sanden

The currently accepted answer cannot grow AND shrink. I think I found a better approach, by defining a custom style with a rectangle (stretched uniformly).

当前接受的答案不能增长和缩小。我想我找到了一个更好的方法,通过使用矩形(均匀拉伸)定义自定义样式。

<Style x:Key="SquareButton" TargetType="Button">
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Background" Value="#3a3a3a" />
    <Setter Property="BorderBrush" Value="#5a5a5a"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">

                <Border HorizontalAlignment="Center">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                        <Rectangle Stretch="Uniform"
                                         Stroke="{TemplateBinding BorderBrush}"
                                         StrokeThickness="4"
                                         Margin="0" 
                                         Fill="{TemplateBinding Background}" 
                                         Panel.ZIndex="1"
                                         />
                        <ContentPresenter Margin="10" 
                           HorizontalAlignment="Center" 
                           VerticalAlignment="Center" 
                           Panel.ZIndex="2" />
                    </Grid>
                    </Border>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="#4a4a4a"/>
        </Trigger>
    </Style.Triggers>
</Style>

Btw, you can also create a perfect round button this way. Just replace Rectangle with Ellipse.

顺便说一句,您也可以通过这种方式创建一个完美的圆形按钮。只需用椭圆替换矩形。

回答by Ethan Shoe

@Dan Pruzey's answer was good, but did not shrink the square if the window was downsized. What I found to work best was to set the height to auto and the width to that height:

@Dan Pruzey 的回答很好,但如果窗口缩小,则正方形不会缩小。我发现最有效的是将高度设置为自动,将宽度设置为该高度:

<Button Height="Auto" Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"/>

This kept the button a square when sizing my window bigger and smaller. Hope this helps someone.

当我的窗口变大和变小时,这使按钮保持方形。希望这可以帮助某人。