TemplateBinding没有传递给ControlTemplate.Resources中创建的VisualBrush
本文关键字:Resources 创建 VisualBrush ControlTemplate TemplateBinding | 更新日期: 2023-09-27 18:16:28
我提前道歉,因为我确定这篇文章之前已经发布过了,我只是似乎不知道如何正确地表达它来找到答案。
我试图创建一个CueTextBox样式模板(所以当它是空的,它显示的工具提示在一个变亮的颜色),但它似乎不会传递{TemplateBinding属性=工具提示}到标签的内容…如果我只是用文本替换它,它工作得很好,但我不能让它绑定到文本框的工具提示。
下面是代码
<Style x:Key="CueTextBox" TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Setter Property="Background" Value="#1e1e1e" />
<Setter Property="Foreground" Value="White" />
<Setter Property="BorderBrush" Value="#434346" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="0">
<ScrollViewer VerticalAlignment="Center" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" x:Name="PART_ContentHost"/>
</Border>
<ControlTemplate.Resources>
<VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Padding="5,0" Content="{TemplateBinding Property=ToolTip}" Foreground="#888888" />
</VisualBrush.Visual>
</VisualBrush>
</ControlTemplate.Resources>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="true" />
<Condition Property="Text" Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource CueBannerBrush}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="true" />
<Condition Property="Text" Value="{x:Static sys:String.Empty}" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource CueBannerBrush}"/>
</MultiTrigger>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="#1e1e1e" />
<Setter Property="BorderBrush" Value="#007acc" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
用法:
<TextBox ToolTip="First Name" Style="{StaticResource CueTextBox}" />
{TemplateBinding Property}
轻量级且快速,但非常有限。复杂场景使用{Binding PropertyPath, RelativeSource={RelativeSource TemplatedParent}}
或{Binding PropertyPath, RelativeSource={RelativeSource AncestorType={x:Type ControlType}}}
可以在绑定上设置PresentationTraceSource.TraceLevel=High
,查看详细的评估日志。
TextBlock
放入模板并切换其可见性?