[ACCEPTED]-How to make button's width autofit the text of a button?-styles

Accepted answer
Score: 26

What you have to do is set the HorizontalAlignment 3 property to Center (or Right or Left). The 2 buttons must be getting stretched by the 1 containing Panel.

   <Style TargetType="{x:Type Button}">
      <Setter Property="MinWidth" Value="90" />
      <Setter Property="HorizontalAlignment" Value="Center" />
   </Style>
Score: 2

If you just set MinWidth of button and if 4 your text width is bigger than min width 3 it will automatically expand, but in reverse 2 case button will be same with your min width,

<Style TargetType="{x:Type Button}">
      <Setter Property="MinWidth" Value="90" />
</Style>

that 1 is enough to archive to required result

Score: 2

If setting HorizontalAlignment isn't working, check to see 5 that you have not set a Width - that will prevent 4 the button from resizing. A handy trick 3 is setting the Width property to NaN or ..NaN depending 2 upon your application's configuration (which 1 ever doesn't throw a compilation error).

More Related questions