How can I get this DataTrigger to work?
Asked Answered
B

1

8

I want my TextBox to have a red background if the ViewModel property = "invalid". What do I have to change so this works?

This version tells me that Background does not have a qualifying type name.

<TextBox
    Width="200"
    Text="{Binding FieldEmail, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.Triggers>
        <DataTrigger Binding="{Binding FieldEmailValidationStatus}" Value="invalid">
            <Setter Property="TextBox.Background" Value="Tomato"/>
        </DataTrigger>
    </TextBox.Triggers>
</TextBox>

When I add "TextBox." it tells me I have to have an EventTrigger:

<TextBox
    Width="200"
    Text="{Binding FieldEmail, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.Triggers>
        <DataTrigger Binding="{Binding FieldEmailValidationStatus}" Value="invalid">
            <Setter Property="Background" Value="Tomato"/>
        </DataTrigger>
    </TextBox.Triggers>
</TextBox>
Bramble answered 19/6, 2009 at 8:44 Comment(0)
B
14

Allow me to answer this one, I had forgotten to wrap it all in a style, then it works nicely:

<TextBox
    Width="200"
    Text="{Binding FieldEmail, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding FieldEmailValidationStatus}" Value="invalid">
                    <Setter Property="TextBox.Background" Value="Tomato"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

or this:

<Style x:Key="FieldEmailStyle" TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding FieldEmailValidationStatus}" Value="invalid">
            <Setter Property="TextBox.Background" Value="Yellow"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding FieldEmailValidationStatus}" Value="valid">
            <Setter Property="TextBox.Background" Value="LightGreen"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

    <TextBox
        Width="200"
        Style="{StaticResource FieldEmailStyle}"
        Text="{Binding FieldEmail, UpdateSourceTrigger=PropertyChanged}">
    </TextBox>
Bramble answered 19/6, 2009 at 8:48 Comment(1)
I had to add TargetType="TextBox" to the <Style> tagFda

© 2022 - 2024 — McMap. All rights reserved.