Skip to content

Instantly share code, notes, and snippets.

@dancrowley303
Created October 17, 2011 01:47
Show Gist options
  • Save dancrowley303/1291751 to your computer and use it in GitHub Desktop.
Save dancrowley303/1291751 to your computer and use it in GitHub Desktop.
style trigger for zero length textbox
Would like to have a xaml (view) only method to change the textbox text to "Search ..." when the textbox is empty.
My xaml file has this:
<TextBox Name="searchBox" Text="{Binding Path=TextFilter, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="Text.Length, {Binding RelativeSource={RelativeSource = Self}}" Value="0">
<Setter Property="Text" Value="Search ..." />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
TextFilter is a string property that may be empty or have a value.
On execution, this error is thrown:
{"'Set property 'System.Windows.DataTrigger.Binding' threw an exception.' Line number '24' and line position '34'."}
Unable to cast object of type 'System.String' to type 'System.Windows.Data.BindingBase'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment