【问题标题】:Setting WPF Button background to text将 WPF 按钮背景设置为文本
【发布时间】:2016-10-05 20:10:57
【问题描述】:

我想在底部有一个带有文本的按钮,但在背景中有更多文本,特别是来自 UI 字体。这样就可以在背景中看到底部的内容文本。

StackPanels 和 DockPanels 似乎没有帮助,因为它们总是为每个控件分配空间,因此它们不会重叠。

类似:

<Button HorizontalContentAlignment="Stretch" VerticalContentAlignment="Bottom" FontFamily="Segoe UI Symbol">
  <Button.Background>
    <TextBox Text="" Foreground="White" Background="#FF5B9BD5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" BorderThickness="0"/>
  </Button.Background>
  <TextBox Text="Text" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" BorderThickness="0"/>
</Button>

此代码不起作用,因为背景不支持控件,仅支持画笔。

【问题讨论】:

  • VisualBrushGrid 面板中的控件可以重叠。不确定我是否理解您想要的布局,屏幕截图可以提供帮助
  • 我认为这是我想要的网格。所需的布局是底部的内容文本,居中,以及按钮中心的“背景”文本,也居中。
  • 谢谢,我明白了:)

标签: wpf button wpf-controls


【解决方案1】:

使用网格可以实现:

<Button VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Foreground="White" FontFamily="Segoe UI Symbol" FontStretch="Expanded" BorderThickness="0">
  <Grid>
    <TextBox Text="" Foreground="White" Background="#FF5B9BD5" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="36" BorderThickness="0"/>
    <TextBox Text="Text" Foreground="White" Background="#FF5B9BD5" HorizontalContentAlignment="Center" VerticalAlignment="Bottom" BorderThickness="0"/>
  </Grid>
</Button>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多