Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evenly-sized buttons according to content of largest button

Tags:

layout

wpf

xaml

Imagine I have two WPF buttons on a window, with content as follows:

<Button>OK</Button> <Button>Cancel</Button> 

I want these buttons to be the same width, however in a scenario where the Content is bound to a localised value for a given user's language, I don't know how wide the buttons need to be to accommodate the new content.

How might I apply a minimum width to these buttons, such that the width of the widest one (according to content) is effectively used as the MinWidth of both, thus keeping them uniform?

Or to put it another way: I don't want the buttons to be the width of their container (unless using a container in a clever way is the answer to my problem), and I don't want them each to just size to their own content because that will make them different sizes. I want something in-between. The one with the largest content to size to display it's content, and all others to size to that same width, so the widths are all equal.

I expect the answer lies in putting them in some sort of container. I know I could use a Grid and let them fill grid "cells", but the point is I don't want them to be too wide, either. I know I could have some code-behind that runs on the Content_Changed event of the buttons and sets the minwidth to that of the widest button, but I'm interested in a pure-xaml method. It might be I need to create a custom control extending ItemsControl that rusn code-behind when new items are added or re-sized and applies the width of the widest item as the MinWidth of all the other items.

Many thanks in advance.

like image 960
Neil Barnwell Avatar asked Jun 06 '11 09:06

Neil Barnwell


People also ask

How do I make all buttons the same size in HTML?

Answers to the following questions. Select a width and height for your button, and use them for the centre of your text horizontally and vertically : width:120px; height:50px; text-assign-center:1:1. It must be 1em, and it should have ble of 1em; font-size:1.

How do I make text fit a button in CSS?

Remove the width and display: block and then add display: inline-block to the button. To have it remain centered you can either add text-align: center; on the body or do the same on a newly created container.


2 Answers

The Grid

  <Grid HorizontalAlignment="Right" Grid.IsSharedSizeScope="true">     <Grid.ColumnDefinitions>         <ColumnDefinition SharedSizeGroup="A"/>         <ColumnDefinition SharedSizeGroup="A"/>     </Grid.ColumnDefinitions>     <Grid.Children>         <Button Grid.Column="0" Content="OK"/>         <Button Grid.Column="1" Content="Cancel"/>     </Grid.Children>   </Grid> 

This can be broken up, you just need to set the IsSharedSizeScope on a common ancestor, e.g.:

    <StackPanel Grid.IsSharedSizeScope="true">         <Grid HorizontalAlignment="Right">             <Grid.ColumnDefinitions>                 <ColumnDefinition SharedSizeGroup="A"/>             </Grid.ColumnDefinitions>             <Grid.Children>                 <Button Grid.Column="0" Content="OK"/>             </Grid.Children>         </Grid>         <!-- ... -->         <Grid HorizontalAlignment="Left">             <Grid.ColumnDefinitions>                 <ColumnDefinition SharedSizeGroup="A"/>             </Grid.ColumnDefinitions>             <Grid.Children>                 <Button Grid.Column="0" Content="Cancel"/>             </Grid.Children>         </Grid>     </StackPanel> 

To prevent the buttons from becoming too large change the HorizontalAlignment of the Grid to something else than Stretch or set a MaxWidth.

like image 196
H.B. Avatar answered Nov 10 '22 14:11

H.B.


Use UniformGrid  <UniformGrid HorizontalAlignment="Right" Rows="1" Columns="2">   <Button Content="Ok" Grid.Column="0"/>   <Button Content="Cancel" Grid.Column="1"/> </UniformGrid> 
like image 43
Amir Touitou Avatar answered Nov 10 '22 13:11

Amir Touitou