[英]Binding to the Title of a Window does not always work
I need to databind two TextBlock
s to the Window.Title
property.我需要将两个
TextBlock
数据绑定到Window.Title
属性。 The first one works via:第一个通过以下方式工作:
RelativeSource FindAncestor, AncestorType=Window}"
But the second one does not (it's deeply nested within a button ToolTip
).但是第二个没有(它深深地嵌套在按钮
ToolTip
)。
How can I change the second one to make it also display the Title
of the Window
?如何更改第二个以使其也显示
Window
的Title
?
<Window ...>
<Border ...>
<Grid ...>
<Grid ...>
<!-- TEXTBLOCK BELOW WORKS -->
<TextBlock Grid.Column="2" Text="{Binding Title, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
HorizontalAlignment="Stretch" VerticalAlignment="Center" Foreground="White" FontSize="18px" FontStretch="UltraExpanded" />
<Button Grid.Column="3" HorizontalAlignment="Right" VerticalAlignment="Stretch"
Background="Transparent" BorderBrush="Transparent" Foreground="Transparent"
ToolTipService.InitialShowDelay="0" ToolTipService.BetweenShowDelay="0" ToolTipService.ShowDuration="60000">
<Button.ToolTip>
<ToolTip x:Name="helpButtonTooltip" Width="240" ToolTipService.InitialShowDelay="0">
<!-- TEXTBLOCK BELOW DOES NOT WORK; HOW CAN I MAKE IT WORK? -->
<TextBlock Text="{Binding Title, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"
HorizontalAlignment="Stretch" VerticalAlignment="Center" Foreground="White" FontSize="18px" FontStretch="UltraExpanded" />
The tool tip is displayed in a popup and is not part of the same visual tree as the button or your window.工具提示显示在弹出窗口中,与按钮或窗口不属于同一可视化树的一部分。 Consequently,
RelativeSource
and ElementName
bindings do not work.因此,
RelativeSource
和ElementName
绑定不起作用。
What you can do is bind the window title to the Tag
property of your button and then bind the Text
of the tool tip TextBlock
to the Tag
property of the PlacementTarget
(which is the button).您可以做的是将窗口标题绑定到按钮的
Tag
属性,然后将工具提示TextBlock
的Text
绑定到PlacementTarget
(即按钮)的Tag
属性。
<Button Tag="{Binding Title, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}">
<Button.ToolTip>
<ToolTip>
<TextBlock Text="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"/>
</ToolTip>
</Button.ToolTip>
</Button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.