0

I'm new to xaml and not sure how databind works. I'm trying to enable hint text when my text box is null. But I'm not sure how the data bind work to disable it when it's not null.

Here is a solution I found that works to get my hint text. How do this code to work to disable it if my first name textbox has a value?

<TextBox Grid.Row="1">
    <TextBox.Style>
        <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <Style.Resources>
                <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                    <VisualBrush.Visual>
                        <Label Content="First Name" Foreground="LightGray" />
                    </VisualBrush.Visual>
                </VisualBrush>
            </Style.Resources>
            <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>
    </TextBox.Style>

    <TextBox.Template>
        <ControlTemplate>
            <TextBox Text="{Binding SelectedItem.FirstName,UpdateSourceTrigger=PropertyChanged}"/>
        </ControlTemplate>
    </TextBox.Template>
</TextBox>
Geoff James
  • 3,122
  • 1
  • 17
  • 36
Van
  • 49
  • 1
  • 8
  • 1
    Possible duplicate of [Watermark / hint text / placeholder TextBox in WPF](http://stackoverflow.com/questions/833943/watermark-hint-text-placeholder-textbox-in-wpf) – John Wu Nov 30 '16 at 18:50
  • I retracted my close vote once I realized you're taking your example from a [previous answer](https://stackoverflow.com/questions/39213504/how-to-apply-cuebanner-for-a-textbox-in-xaml) of mine. Do you just want to make it disabled IF value = not null? – Chris W. Nov 30 '16 at 19:30
  • I just wanted to bind the text. I figured out by just putting the bind at the top of the – Van Nov 30 '16 at 20:48

1 Answers1

0

Never mind I figured out.

I could set the textbox binding like:

<TextBox Text="{Binding SelectedItem.FirstName,
                        UpdateSourceTrigger=PropertyChanged}"
         Grid.Row="1">

I didn't know you could do that if you had an opening and closing <textbox> and </textbox> tags.

I thought you could only do it if the textbox has a self-closing tag (<textbox />).

Sorry for the silly question.

Eugene
  • 2,858
  • 1
  • 26
  • 25
Van
  • 49
  • 1
  • 8