3

I'm implementing a search textbox; could you please help me with binding to TextBox.Tag?

Style

<Style x:Key="SearchTextBox" TargetType="{x:Type TextBox}">
      <Style.Resources>
       <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
          <VisualBrush.Visual>
            <Label Content="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" Foreground="{StaticResource SearchTextBox.Foreground}" FontSize="{StaticResource SearchTextBox.FontSize}"/>
          </VisualBrush.Visual>
        </VisualBrush>
      </Style.Resources>
      <Setter Property="FontSize" Value="{StaticResource SearchTextBox.FontSize}" />
      <Setter Property="Foreground" Value="{StaticResource SearchTextBox.TextForeground}" />
      <Setter Property="MinWidth" Value="200" />
          <Style.Triggers>
            <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
              <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
            </Trigger>
            <Trigger Property="Text" Value="{x:Null}">
              <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
            </Trigger>
            <Trigger Property="IsKeyboardFocused" Value="True">
              <Setter Property="Background" Value="White" />
            </Trigger>
          </Style.Triggers>
    </Style>

Usage

<TextBox Style="{StaticResource SearchTextBox}" Tag="Search templates" />

How can I get the binding to work?

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
Siarhei Kuchuk
  • 5,296
  • 1
  • 28
  • 31

1 Answers1

3

This article here is extremely similar to yours: WPF Bind to parent property from within nested element using style

Though, it doesn't really give a code sample, so here's some xaml you can use as an alternative to your current approach.

<Style x:Key="SearchTextBox" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}">
  <Style.Setters>
    <Setter Property="Tag" Value=""/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TextBox}">
          <Grid>
            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
            <TextBlock x:Name="textBlock" Opacity="0.345" Text="{TemplateBinding Tag}" TextWrapping="Wrap" Visibility="Hidden" />
          </Grid>
          <ControlTemplate.Triggers>
            <MultiTrigger>
              <MultiTrigger.Conditions>
                <Condition Property="IsFocused" Value="False" />
                <Condition Property="Text" Value="" />
              </MultiTrigger.Conditions>
              <Setter Property="Visibility" TargetName="textBlock" Value="Visible" />
            </MultiTrigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style.Setters>
</Style>

And you'll still write your textbox code the same way you already had it:

<TextBox Style="{StaticResource SearchTextBox}" Tag="Search templates" />
Community
  • 1
  • 1
Bill Tarbell
  • 4,933
  • 2
  • 32
  • 52
  • You're welcome. As Samuel Jack stated in the link i provided, if you use resources in the style then they are only instantiated once. So, if you had multiple textboxes then they'd always have the same 'hint' text. – Bill Tarbell Nov 11 '12 at 21:43
  • Yea, i noticed that! my first implementation was via attached property and it had default text!)))) Thank you again. – Siarhei Kuchuk Nov 11 '12 at 21:48
  • Thanks again for your brilliant answer. Everything works now so coool) Thank you mister Guru) – Siarhei Kuchuk Nov 14 '12 at 20:27
  • Thanks again.... I'm just add the same thing to other thing and it works soo cool so awesome))) – Siarhei Kuchuk Nov 18 '12 at 12:19