wpf WPF使用样式从嵌套元素内绑定到父属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3639537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:05:41  来源:igfitidea点击:

WPF Bind to parent property from within nested element using style

wpfstylesrelativesource

提问by veljkoz

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?

那么问题是什么?如何从样式中绑定到祖先属性?

采纳答案by Samuel Hyman

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,而在于您使用 VisualBrush 的方式。回想一下,样式在您应用它们的元素之间共享。您的示例不起作用的原因是,实际上您正在尝试与多个父文本框共享单个文本框(您标记为“内部”的文本框)。

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?

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

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

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

回答by vortexwolf

Reativesource doesn't work as expected. It is better to create watermark textbox using control template. But your version could work:

Reativesource 没有按预期工作。最好使用控件模板创建水印文本框。但你的版本可以工作:

<Window.Resources>
    <Style TargetType="TextBox" x:Key="stlHintbox">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Mode=Self}}" Value="">
                <Setter Property="TextBox.Background">
                    <Setter.Value>
                        <VisualBrush Stretch="None" Visual="{Binding ElementName=hintText}"/>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<StackPanel>
    <TextBox Tag="hint text" x:Name="myTextBox" Style="{StaticResource stlHintbox}" />
    <Border Visibility="Hidden">
        <TextBlock x:Name="hintText" Text="{Binding Tag, ElementName=myTextBox}" FontStyle="Italic" Foreground="LightGray" />
    </Border>
</StackPanel>