WPF - 风格中的相对源

标签 wpf xaml binding styles relativesource

我的目标是绑定(bind)内容 - 的标签属性标签 - 应用样式的元素的属性。但我的解决方案似乎不起作用:

我的风格:

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

我的文本框:
<TextBox
   Style="{StaticResource ResourceKey=HintedTextBox}"
   x:Name="tbTest" />

最佳答案

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

你可以这样做:

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

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

1. 你可能明白,看看我的例子,RelativeSource必须不是self,在这种情况下它将指向自己(VisualBrush),并且类型必须是TextBox的元素,位于视觉树的较高位置。

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

3. 在样式中,这种结构不起作用,因为 Style只是setter的集合,他不懂控制,有没有。为此,通常使用 DataTemplateControlTemplate .

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

下面是我的例子:
<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

enter image description here

关于WPF - 风格中的相对源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18431043/

相关文章:

c# - 如何以原始格式保存图像?

wpf - 通过键绑定(bind)从 Datagrid 传递命令参数

Swift MVVM 绑定(bind)拳击

winforms - 将具有主/详细信息的类绑定(bind)到两个 datagridview 中

php - 用 OR 条件绑定(bind)参数 Mysql

wpf - WPF:扩展ListView的GridView的最后一列

wpf - 在 WPF 中的像素着色器/其他方法中重新创建 <BevelBitmapEffect>

wpf - 使用mvvm实现点击事件

c# - 如何将 xaml 中的值分配给不可绑定(bind)的属性

c# - 样式 LonglistMultiselector windows phone 8