【问题标题】:Stretching not applying in a WPF templated Button拉伸不适用于 WPF 模板化按钮
【发布时间】:2013-04-03 18:57:06
【问题描述】:

我在 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>

我在我的窗口中使用这个按钮:

<Button x:Name="BtnRestoreDefault" Template="{DynamicResource BoutonGris}" Content="Rest. défaut" Height="27"  Width="88" Click="Button_Click_RestoreDefault"/>

我的问题是按钮似乎没有 Height="27" 和 Width="88"。

这是我在 VS2012 中的结果:

按钮大小合适,但灰色区域没有。我在模板上使用了 Stretch 关键字,但没有发现错误。

如果我对 Horizo​​ntalAlignment、Horizo​​ntalContentAlignment、VerticalAlignment 和 VerticalContentAlignment 使用 Stretch,灰色的大小合适,但文本内容在左/上!我想要一个中心文本。

请问有人有意见吗?

非常感谢,

最好的问候,

尼克斯

【问题讨论】:

  • 想要的外观是什么?背景应该完全填满吗?
  • 是的,我想要:按钮大小 = 背景大小(灰色区域大小)

标签: c# wpf xaml templates button


【解决方案1】:

这应该可以解决问题。

<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>

一个问题,为什么你的ControlTemplate 中有一个Button 用于Button?通常,我希望在这里使用像 Border 等简单的控件,因为您想重新实现视觉外观。

编辑

ContentPresenter 之一应该这样声明。

<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>

我不确定是哪一个。但你可以同时尝试。 ;o)

【讨论】:

  • 如果我对 Horizo​​ntalAlignment、Horizo​​ntalContentAlignment、VerticalAlignment 和 VerticalContentAlignment 使用 Stretch,灰色的大小合适,但文本内容在左/上!我想要一个中心文本。 :(
  • 非常感谢!完美的 ! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 2013-03-21
相关资源
最近更新 更多