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

时间:2022-08-27 12:07:50

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时,它不会:

<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?

那捕获的是什么?如何从样式中绑定到祖先属性?

2 个解决方案

#1


12  

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 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中存在DataTemplates和ControlTemplates的原因。它们不是直接实际显示视觉效果,而是定义一个模板,允许根据需要创建多个视觉副本。

#2


5  

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>

#1


12  

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 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中存在DataTemplates和ControlTemplates的原因。它们不是直接实际显示视觉效果,而是定义一个模板,允许根据需要创建多个视觉副本。

#2


5  

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>