拉伸不适用于 WPF 模板化按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15951180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stretching not applying in a WPF templated Button
提问by Walter Fabio Simoni
I created a templated button in WPF :
我在 WPF 中创建了一个模板化按钮:
<ControlTemplate TargetType="{x:Type Button}"
x:Key="BoutonGris">
<Button Foreground="White"
BorderBrush="Transparent"
HorizontalAlignment="Center"
VerticalAlignment="Center"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch">
<Button.Content>
<Border CornerRadius="3"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<ContentPresenter />
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="Background"
Value="#58585a" />
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Background"
Value="{DynamicResource DegradeCouleurTheme}" />
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</Button.Content>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
</ControlTemplate>
I use this button in my Window :
我在我的窗口中使用这个按钮:
<Button x:Name="BtnRestoreDefault" Template="{DynamicResource BoutonGris}" Content="Rest. défaut" Height="27" Width="88" Click="Button_Click_RestoreDefault"/>
My problem is that the button don't appear to have the Height="27" and Width="88".
我的问题是按钮似乎没有 Height="27" 和 Width="88"。
Here is my result in VS2012 :
这是我在 VS2012 中的结果:


The button has the good size, but the gray area don't. I have use the Stretch keywords on the Template but i don't find the mistake.
按钮大小合适,但灰色区域没有。我在模板上使用了 Stretch 关键字,但我没有发现错误。
If i use Stretch for HorizontalAlignment, HorizontalContentAlignment, VerticalAlignment, and VerticalContentAlignment, the gray has the good size, but, the text content is in left/up ! I would like a center text.
如果我将 Stretch 用于 HorizontalAlignment、HorizontalContentAlignment、VerticalAlignment 和 VerticalContentAlignment,则灰色具有良好的尺寸,但是,文本内容在左/上!我想要一个中心文本。
Anyone have an idea please ?
有人有想法吗?
Thanks a lot,
非常感谢,
Best regards,
此致,
Nixeus
尼克斯
回答by DHN
This should do the trick.
这应该可以解决问题。
<ControlTemplate TargetType="{x:Type Button}" x:Key="BoutonGris">
<Button Foreground="White" BorderBrush="Transparent"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" >
<Button.Content>
<!-- A lot of your code -->
</Button.Content>
<!-- A lot of your code -->
</Button>
</ControlTemplate>
One question, why do you have a Buttonin your ControlTemplatefor a Button? Usually, I would expect simple controls like Borderetc here, because you want to reimplement the visual appearance.
一个问题,为什么Button你的ControlTemplatefor a 中有 a Button?通常,我希望像Border这里这样的简单控件,因为您想重新实现视觉外观。
Edit
编辑
One of the ContentPresentershould be declared like this.
其中之一ContentPresenter应该像这样声明。
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
I'm not sure which one. But you can try both. ;o)
我不确定是哪一个。但是您可以同时尝试两者。;o)

