WPF - 样式中的相对源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18431043/
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-13 09:29:44  来源:igfitidea点击:

WPF - RelativeSource in Style

wpfxamlbindingstylesrelativesource

提问by Cubi73

My target is to bind the Content-Property of the Labelto the Tag-Property of the Elements the Style is applied to. But my solution doesn't seem to work:

我的目标是将标签Content-Property绑定到应用样式元素的Tag-Property 。但我的解决方案似乎不起作用:



My style:

我的风格:

<Style
   TargetType="TextBox"
   x:Key="HintedTextBox">
   <Style.Resources>
      <VisualBrush
         AlignmentX="Left"
         AlignmentY="Center"
         Stretch="None"
         x:Key="HintedTextBox_Hint">
         <VisualBrush.Visual>
            <Label
               Content="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"
               Foreground="LightGray" />
         </VisualBrush.Visual>
      </VisualBrush>
   </Style.Resources>
   <!-- Triggers -->
</Style>

My textbox:

我的文本框:

<TextBox
   Style="{StaticResource ResourceKey=HintedTextBox}"
   x:Name="tbTest" />

采纳答案by Anatoliy Nikolaev

If I understand correctly, you want to set the text for VisualBrush, that will be displayed in the TextBox.

如果我理解正确,您想为 设置文本VisualBrush,该文本将显示在TextBox.

You can do it like this:

你可以这样做:

<TextBox Name="MyTextBox" Tag="MyNewValue" Width="100" Height="25">
    <TextBox.Background>
        <VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="None">
            <VisualBrush.Visual>
                <Label Content="{Binding RelativeSource={RelativeSource AncestorType=TextBox}, Path=Tag}" Foreground="LightGray" />
            </VisualBrush.Visual>
        </VisualBrush>
    </TextBox.Background>
</TextBox>

In order to explain why your example not earned:

为了解释为什么你的例子没有赢得:

1.As you probably understand, looking at my example, RelativeSourcemust be notself, in which case it will point to itself (VisualBrush), and the element with the type must be of TextBox, located higher in the visual tree.

1.正如你可能理解的,看我的例子,RelativeSource一定不是self,在这种情况下它会指向自己 ( VisualBrush),并且类型必须是 of 的元素TextBox,位于可视化树的更高位置。

2.Binding with RelativeSourcedoes not work in resources, because the Resourceis not part of the visual tree, or part of the template.

2.绑定RelativeSource在资源中不起作用,因为Resource它不是可视化树的一部分,也不是模板的一部分。

3.In styles this construction will not work, because the Styleis just the collection of setters, he does not know about control, are there. For this purpose, usually using DataTemplateor ControlTemplate.

3.在样式中,这种构造不起作用,因为Style它只是 setter 的集合,他不知道控制,在那里。为此,通常使用DataTemplateControlTemplate

As an alternative, in this case, I suggest using a template for the TextBox, which will be registered VisualBrush.

作为替代方案,在这种情况下,我建议为 使用模板,该模板TextBox将被注册VisualBrush

Below is my example:

下面是我的例子:

<Window.Resources>            
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="MinWidth" Value="120" />
        <Setter Property="MinHeight" Value="20" />
        <Setter Property="AllowDrop" Value="true" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBoxBase}">
                    <Border Name="Border" CornerRadius="0" Padding="2" BorderThickness="1" BorderBrush="Black">
                        <Border.Background>
                            <VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="None">
                                <VisualBrush.Visual>
                                    <Label Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}"
                                           Foreground="LightGray" />
                                </VisualBrush.Visual>
                            </VisualBrush>
                        </Border.Background>

                        <ScrollViewer Margin="0" x:Name="PART_ContentHost" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid>
    <TextBox Name="MyTextBox" Tag="MyNewValue" Width="100" Height="25" />        
</Grid>

Output

Output

enter image description here

在此处输入图片说明