Set button's width to star

时间:2015-07-08 15:51:58

标签: wpf

I have a button with a custom style used in all my application. This style has the following property :

<Setter Property="Width" Value="150" />

But on a particular page I need to have my button stretched to its parent. I would like this to do something like to "cancel" the 150 width :

<Button Style="{StaticResource MyStyle}" Width="*" />

How can I achieve this ?

2 个答案:

答案 0 :(得分:1)

尝试默认值:Double.NaN。在XAML中它是:

Width="Auto"

来自FrameworkElement.Width Property的评论:

  

除了可接受的Double值之外,此属性也可以   Double.NaN。这是您指定自动调整大小行为的方式。在XAML中你   将值设置为字符串“Auto”(不区分大小写)以启用   自动调整大小的行为。自动调整大小行为意味着该元素   将填充可用的宽度。

答案 1 :(得分:0)

<Window.Resources>
    <Style TargetType="Button" x:Key="MyStyle">
        <Setter Property="Width" Value="150" />
    </Style>
</Window.Resources>


  <Button Content="fdfdfg">
      <Button.Style>
        <Style TargetType="Button" BasedOn="{StaticResource MyStyle}">
            <Setter Property="Width" Value="Auto"/>
        </Style>
      </Button.Style>          
  </Button>