WPF: equal length ToggleButtons string?

I am currently displaying RadioButtons in my application as ToggleButtons strings (see my last question ). However, I would like the buttons to have the same width - currently each button is as wide as it should be.

Since I work with templates, I would like to avoid specifying a width every time I use a control, if possible - instead, the width of each button in a row should be equal to the width of the widest button in this group.

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

+3
source share
4 answers

Perhaps by using UniformGridand setting an int style that will help the property HorizontalAlignement="Stretch".

+7
source

If you have access to all the toggle buttons (for example, they are not tied to the database), then you can do a neat trick by linking the minimum width of each button with the width of the adjacent one. When the last button is attached 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>
+4
source

, ToggleButtons , .

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

1 ​​ ,

0

Source: https://habr.com/ru/post/1757338/


All Articles