【发布时间】:2016-02-23 12:47:08
【问题描述】:
我想知道是否有人可以提供帮助,我正在为我正在处理的应用程序设计一个自定义 WPF 窗口,但我遇到了 Min、Max 和 Close 按钮的问题。我为 3 个按钮设计了一个 ControlTemplate,它们位于 StackPanel 和 Vertical 方向。在我的基本风格中,我有以下内容
<Style x:Key="BaseWindowButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value={Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" />
<Setter Property="Background" Value="Transparent" />
...
</Style>
还尝试将前景设置为特定颜色,例如#FF000000,但没有任何显示
我有一个样式继承了按钮的这种样式,但不改变前景或背景。
我的问题是按钮内容不显示,按钮显示,IsMouseOver 触发器触发改变背景,但文本内容从不显示。
最小按钮样式是
<Button x:Name="PART_Min">
<Path Fill="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"
Data="F1M0,6L0,9 9,9 9,6 0,6z"
SnapsToDevicePixels=True" />
</Button>
我不知道为什么内容不显示,所以很感激你的想法。
下面是可视化树,我已经检查了它并确定了前景值
Window (Foreground: #FF000000)
Grid
AdornerDecorator
Grid
ContentControl (Foreground: #FF000000)
StackPanel
Button (Foreground: #FF000000)
Grid
ContentControl (Foreground: #FF000000)
但是就像我上面说的,我已经删除了绑定并指定了一个物理值,但仍然没有显示内容
【问题讨论】:
-
与
TextElement.Foreground的绑定解析为什么值? -
我是 WPF 新手,所以我的理解有限,但相对源绑定将是类型为 FrameworkElement 的 Button 的基本类型
-
{RelativeSource AncestorType={x:Type FrameworkElement}}将使用元素树中更高层的FrameworkElement,不是继承树。这很可能是 Button 的父元素。使用 Visual Studio 的“Live Visual Tree”查找绑定值。 -
使用已命名的
TextBlock或Label代替<Setter Property="Foreground" Value={Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground)}" />。