Я пытаюсь собрать простой TextBox с текстом водяного знака в Background. Мой код основан на примере из блог Филипа Патрика.
Я пытаюсь настроить его так, чтобы текст, отображаемый в фоновом режиме, извлекался из свойства ToolTip в файле TextBox.
В настоящее время это работает:
<TextBox ToolTip="Type a name here...">
<TextBox.Background>
<VisualBrush TileMode="None" Opacity="0.4" Stretch="None" AlignmentX="Left">
<VisualBrush.Visual>
<TextBlock FontStyle="Italic" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TextBox}}, Path=ToolTip}"/>
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Background>
</TextBox>
Это отображает текст ToolTip в Background файла TextBox.
Но если я перенесу часть кода в стиль ресурсов, привязка больше не будет получать информацию ToolTip из TextBox:
<Grid>
<Grid.Resources>
<Style x:Key="WatermarkBackground" TargetType="{x:Type TextBox}">
<Setter Property="Background">
<Setter.Value>
<VisualBrush TileMode="None" Opacity="0.4" Stretch="None" AlignmentX="Left">
<VisualBrush.Visual>
<TextBlock FontStyle="Italic" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TextBox}}, Path=ToolTip}"/>
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<TextBox ToolTip="Type your name here..." Style="{StaticResource WatermarkBackground}"/>
Any tips here?