2012-07-24 87 views
0

嗨,我有一个按钮风格retemplates这样按钮的宽度相对于其contentpresenters宽度

<Style x:Name="NoStyleButton" TargetType="Button">  
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 
        <ContentPresenter x:Name="ContentPresenterInButton"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 

问题的风格是我想要的按钮的宽度是相同的作为按钮内的内容。如果我把一个带有这种样式的按钮放在一个堆栈面板中,按钮的宽度与堆栈面板的宽度相同,当按钮内容很少时(Think small TextBlock)会导致问题,用户可以按下按钮,即使他们点击按钮内的TextBlock。

我想你可以做同样的事情到

<Setter Property="Width" Value="{Binding ActualWidth, ElementName=ContentPresenterInButton}"/> 

但是,这并不工作。

有什么想法?

回答

1

的问题是,它是决定你的按钮的宽度,而不是内容的StackPanel。您可以设置按钮水平对齐方式,以便它不拉伸以填充可用宽度:

<StackPanel Orientation="Vertical"> 
    <Button HorizontalAlignment="Left">foo</Button> 
    <Button>bar</Button> 
</StackPanel> 

enter image description here

+0

当然要归功于 – Jea 2012-07-24 09:06:41

0

与尝试:

<Button HorizontalAlignment="Stretch">Text</Button>