如何使按钮的宽度自动调整按钮的文本?

Ale*_*sky 12 .net wpf styles

我目前正在这样做,但我做错了:):

   <Style TargetType="{x:Type Button}">
      <Setter Property="MinWidth" Value="90" />
      <Setter Property="Width" Value="Auto" />
   </Style>
Run Code Online (Sandbox Code Playgroud)

我希望按钮具有一些最小宽度,但也要使其宽度扩展以适合按钮的文本.

Ale*_*sky 17

您需要做的是将Horizo​​ntalAlignment属性设置为Center(或Right或Left).按钮必须由包含的面板拉伸.

   <Style TargetType="{x:Type Button}">
      <Setter Property="MinWidth" Value="90" />
      <Setter Property="HorizontalAlignment" Value="Center" />
   </Style>
Run Code Online (Sandbox Code Playgroud)