WPF使用样式从嵌套元素内绑定到父属性 [英] WPF Bind to parent property from within nested element using style

查看:36
本文介绍了WPF使用样式从嵌套元素内绑定到父属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试构建一个带有提示的文本框,该提示在它为空时显示.我在样式中设置提示文本时遇到问题.

I've been trying to build a text box with a hint that's displaying while it's empty. I'm having trouble setting the hint text from within a style.

准确地说,这有效(也就是说,它正确绑定):

To be precise, this works (that is, it binds correctly):

    <TextBox Tag="hint text">
        <TextBox.Background>
            <VisualBrush Stretch="None">
                <VisualBrush.Visual>
                    <TextBlock Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=TextBox}}" FontStyle="Italic" Foreground="LightGray" />
                </VisualBrush.Visual>
            </VisualBrush>
        </TextBox.Background>
    </TextBox>

但是,当我将它移动到样式时,它不会:

but, when I move it to the Style, it doesn't:

<Style TargetType="TextBox" x:Key="stlHintbox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Mode=Self}}" Value="">
            <Setter Property="Background">
                <Setter.Value>
                    <VisualBrush Stretch="None">
                        <VisualBrush.Visual>
                            <TextBlock Tag="inner" Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=TextBox}}" 
                                       FontStyle="Italic" Foreground="LightGray" />
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

<TextBox Tag="hint text" Style="{StaticResource stlHintbox}" />

那么有什么收获呢?如何从样式中绑定到祖先属性?

So what's the catch? How can I bind to ancestor property from within a style?

推荐答案

问题不在于 RelativeSource,而在于您使用 VisualBrush 的方式.回想一下,样式在您应用它们的元素之间共享.您的示例不起作用的原因是,实际上您正在尝试与多个父文本框共享单个文本框(您标记为内部"的文本框).

The problem is not with the RelativeSource but with the way you are using the VisualBrush. Recall that Styles are shared between the elements you apply them to. The reason that your example doesn't work is that, in effect you are trying to share a single textbox (the one you tagged "inner") with multiple parent textboxes.

要了解为什么会出现这个问题,请尝试一个思想实验:内部文本框创建一次(粗略地说,这将在创建样式时发生).使用 RelativeSource 绑定时,应该选择应用样式的哪个文本框作为内部文本框的祖先?

To see why this is a problem, try a thought experiment: The inner textbox gets created once (roughly speaking, this will happen when the style is created). Which of the textboxes that the style gets applied to should be chosen as the ancestor of the inner text box when you use the RelativeSource binding?

这就是为什么 DataTemplatesControlTemplates 存在于 WPF 中.他们没有直接实例化视觉对象,而是定义了一个模板,允许根据需要创建多个视觉对象副本.

This is why DataTemplates and ControlTemplates exist in WPF. Rather than actually instantiate visuals directly, they define a template that allow multiple copies of visuals to be created as needed.

这篇关于WPF使用样式从嵌套元素内绑定到父属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆