C# 拉伸不应用于WPF模板化按钮
我在WPF中创建了一个模板按钮:C# 拉伸不应用于WPF模板化按钮,c#,wpf,xaml,templates,button,C#,Wpf,Xaml,Templates,Button,我在WPF中创建了一个模板按钮: <ControlTemplate TargetType="{x:Type Button}" x:Key="BoutonGris"> <Button Foreground="White" BorderBrush="Transparent" HorizontalAlignment="Center" VerticalAlignment="Cent
<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>
我在窗口中使用此按钮:
<Button x:Name="BtnRestoreDefault" Template="{DynamicResource BoutonGris}" Content="Rest. défaut" Height="27" Width="88" Click="Button_Click_RestoreDefault"/>
我的问题是,按钮似乎没有高度=“27”和宽度=“88”
以下是我在VS2012中的结果:
<Button x:Name="BtnRestoreDefault" Template="{DynamicResource BoutonGris}" Content="Rest. défaut" Height="27" Width="88" Click="Button_Click_RestoreDefault"/>
按钮大小合适,但灰色区域不合适。我在模板上使用了拉伸关键字,但我没有发现错误
如果我对水平对齐、水平内容对齐、垂直对齐和垂直内容对齐使用“拉伸”,则灰色具有良好的大小,但文本内容位于左/上方向!我想要一个中间文本 有人有主意吗 非常感谢 致以最良好的祝愿
尼克斯这应该能奏效
<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>
我不知道是哪一个。但你可以两种都试试;o) 理想的外观是什么?背景是否应完全填充?是的,我希望:按钮大小=背景大小(灰色区域大小)如果我使用拉伸进行水平对齐、水平内容对齐、垂直对齐和垂直内容对齐,则灰色的大小较好,但文本内容为左/上!我想要一个中间文本:(非常感谢!太好了!)