简体   繁体   English

WPF:ToggleButtons的行长度相等?

[英]WPF: Row of ToggleButtons with equal length?

I'm currently displaying RadioButtons in my application as rows of ToggleButtons (see my last question ). 我当前正在我的应用程序中显示RadioButtons作为ToggleButtons的行(请参阅我的上一个问题 )。 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? 有任何想法如何在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. 在您的资源部分中创建一个以ToggleButtons为目标的样式,并将宽度设置为您想要的任何值。

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

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

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

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