Andrew Simpson
Andrew Simpson

Reputation: 7324

My data trigger does not trigger

I have a TextBox and I have a button.

The textbox value is bound to my view model.

My button is bound to my view model.

When the user clicks the button, I want the text to display 'yes' if the text box is empty.

Everything seems to work but the trigger does not seem to invoke the changes.

This is my UI:

<TextBox Name="txtFirstName" 
         Text="{Binding BookingWizard.CustomerRecord.FName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                    <DataTrigger Binding="{Binding BookingWizard.IsCustomerFNameValid}" Value="False">
                        <Setter Property="Text" Value="yes" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

My View model:

public ICommand ValidateForm
{
    get
    {
        if (_validateForm == null)
        {
            _validateForm = new BookingCommand(
                  param => Validate()         
            );
        }
        return _validateForm;
    }
}
private void Validate()
{
    if (CustomerRecord.FName.Trim()=="")
    {
        IsCustomerFNameValid = false;
    }
    else
    {
        IsCustomerFNameValid = true;
    }
}

public bool IsCustomerFNameValid 
{
     get { return _IsCustomerFNameValid; } 
     set { 
        _IsCustomerFNameValid = value; 
        RaisePropertyChanged("IsCustomerFNameValid"); 
     } 
}

when I debug I can see that IsCustomerFNameValid is set to false but the textbox does not show 'yes'.

Upvotes: 0

Views: 203

Answers (1)

Joel Lucsy
Joel Lucsy

Reputation: 8706

You are setting the text directly as an attribute of the TextBlock tag. This will override any value from style. Move it the style itself:

 <TextBox Name="txtFirstName">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Setter Property="Text" Value="{Binding BookingWizard.CustomerRecord.FName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding BookingWizard.IsCustomerFNameValid}" Value="False">
                        <Setter Property="Text" Value="yes" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

Upvotes: 1

Related Questions