[英]Stretching not applying in a WPF templated Button
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 ! 如果我使用Stretch进行HorizontalAlignment,HorizontalContentAlignment,VerticalAlignment和VerticalContentAlignment,则灰色具有良好的大小,但是,文本内容在左/上! I would like a center text.
我想要一个中心文字。
Anyone have an idea please ? 有人有想法吗?
Thanks a lot, 非常感谢,
Best regards, 最好的祝福,
Nixeus Nixeus
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 Button
in your ControlTemplate
for a Button
? 有一个问题,为什么你的
ControlTemplate
有一个Button
用于Button
? Usually, I would expect simple controls like Border
etc here, because you want to reimplement the visual appearance. 通常,我希望在这里使用像
Border
这样的简单控件,因为你想重新实现视觉外观。
Edit 编辑
One of the ContentPresenter
should be declared like this. 其中一个
ContentPresenter
应该像这样声明。
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
I'm not sure which one. 我不确定是哪一个。 But you can try both.
但你可以尝试两者。 ;o)
; O)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.