TemplateBinding没有传入ControlTemplate.Resources中创建的VisualBrush

时间:2014-08-05 22:31:30

标签: c# wpf wpf-controls

我提前道歉,因为我确定之前已经发布过,我似乎无法弄清楚如何正确地说出来找到答案。

我正在尝试为CueTextBox创建一个样式模板(所以当它为空时,它会以浅色显示工具提示)但似乎不会将{TemplateBinding Property = ToolTip}传递给CueTextBox的内容label ...如果我只是用文本替换它,它可以正常工作,但是我无法将它绑定到Textbox的工具提示。

这是代码

<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}" />

1 个答案:

答案 0 :(得分:1)

{TemplateBinding Property}轻巧而快速,但非常有限。在复杂方案中,请使用{Binding PropertyPath, RelativeSource={RelativeSource TemplatedParent}}{Binding PropertyPath, RelativeSource={RelativeSource AncestorType={x:Type ControlType}}}

您可以在绑定上设置PresentationTraceSource.TraceLevel=High以查看详细的评估日志。

顺便说一句,我不明白你为什么要使用画笔...为什么不把TextBlock放入模板并切换其可见度呢?