简体   繁体   中英

WPF: Row of ToggleButtons with equal length?

I'm currently displaying RadioButtons in my application as rows of ToggleButtons (see my last question ). However, I'd like the buttons to be of the same width - currently, every button is just as wide as it has to be.

Since I'm working with templates, I'd like to avoid specifying the width every time I use the control if possible - instead, the width of every button in the row should be equal to that of the widest button in that group.

Any ideas how to do this in XAML? :-)

也许使用UniformGrid并在样式中设置HorizontalAlignement="Stretch"属性将有所帮助。

If you have access to all the toggle buttons (eg they aren't databound) then there is a neat trick you can do by binding the minwidth of each button to the width of the one next to it. With the final button being bound to the first:

<StackPanel Orientation="Horizontal">
    <Button x:Name="Button1" Content="Long text" MinWidth="{Binding ElementName=Button2, Path=ActualWidth}"/>
    <Button x:Name="Button2" Content="A" MinWidth="{Binding ElementName=Button3, Path=ActualWidth}"/>
    <Button x:Name="Button3" Content="Extremely long text that should cause this button to be really wide" MinWidth="{Binding ElementName=Button1, Path=ActualWidth}"/>
</StackPanel>

in your resources section create a style that targets ToggleButtons and sets the width to whatever value you want.

<Window.Resources>
    <Style TargetType="{x:Type ToggleButton}">
        <Setter Property="Width" Value="50" />
    </Style>
</Window.Resources>

在每列中使用带有1个按钮的网格,并像这样定义宽度

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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