Binding a Data Trigger

Hi guys,
I have a XAML page with a label which Text property is binding with a ViewModel property named SALDO.
I want to see the label's TextColor Red when the value from SALDO is <0.
I have tried this, there is no compilation error, but the TextColor never change.

    <Label
      Style="{StaticResource TextoGeneral}"
      Text="{Binding Saldo, StringFormat='{0:C0}'}">
      <Label.Triggers>
        <DataTrigger TargetType="Label"
                     Binding="{Binding Saldo}"
                     Value="<0">
          <Setter Property="TextColor" Value="Red" />
        </DataTrigger>
      </Label.Triggers>      
    </Label>

Any idea?
Tanks

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @Pino13
    You can't put in calculation in a condition. Its boolean: Either the condition is true or false.

    for calculation you'll have to make a Converter to do the math then return true or false.

  • Pino13Pino13 COMember

    @ClintStLaurent
    The calculation for SALDO is in the ViewModel .
    The boolean trigger is "<0" isnt it?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited January 2017

    @Pino13

    No. "Is less than 0" is a calculation to be evaluated. Something either is 0 or is not 0. That's boolean.

    You can't enter a formula in a string and expect the math to be evaluated by magic.

    You can't set a label to "5 + 4 + 6 +8" and expect it to display "23" Just doesn't work that way.
    Same here "<3" requires an evaluation calculation to be performed. That requires executable code in a C# Converter. It is not done in XAML markup.

    Just like you can't set Margin = "5 + 6" and expect a margin of 11.

  • Pino13Pino13 COMember

    @ClintStLaurent
    Ok, got it.
    Im gonna try something based on your explanation and ill be back.
    Tanks !

  • Pino13Pino13 COMember

    Now Im doing this on ViewModel

            if (Saldo <0)
            {
                suicheSaldo = true;
            }
            else
            {
                suicheSaldo = false;
            }
    

    and this on XAML

        <Label
          Style="{StaticResource TextoGeneral}"
          Text="{Binding Saldo, StringFormat='{0:C0}'}">
          <Label.Triggers>
            <DataTrigger TargetType="Label"
                         Binding="{Binding suicheSaldo}"
                         Value="true">
              <Setter Property="TextColor" Value="{StaticResource ColorRealce}" />
            </DataTrigger>
          </Label.Triggers>
        </Label>
    

    Still no change on TextColor :neutral:

  • Look at this working example as a template:

                  <Label Text="{Binding DisplayText}" TextColor="Black">
                    <Label.Triggers>
                      <DataTrigger TargetType="Label" Binding="{Binding HasExpired}" Value="True">
                        <Setter Property="TextColor" Value="Red"/>
                      </DataTrigger>
                    </Label.Triggers>
                  </Label>
    
Sign In or Register to comment.