Share via


Convenience Rules

Convenience rules are predefined inline rules that are used for common condition-action combinations for one source and one target.

  • A Default rule has an action but no condition. Default rules are used to set a default state when no other rules apply. They can also be used to perform a one-time initialization of state when the UI is first created.

    The following example sets the color of Label to purple:

    <Rules>
        <Default Target="[Label.Color]" Value="Purple" />
    </Rules>
    

    Default rules are added for you automatically when you set a value in the view item and have another rule referring to the same target.

    For example, you have a view item such as <Text Color="Yellow" />. You also have a rule that reacts to focus and sets the text color to Red. You don't need to include a default rule to revert the text color to Yellow when the focus is gone because a default rule to do this was added for you automatically.

  • A Binding rule tracks changes to a Source value and applies it to a Target.

    The following example binds the value of Value to Label so that when Value changes, it is automatically reflected in the Label text:

    <Rules>
        <Binding Source="[Value.ToString]" Target="[Label.Content]" />
    </Rules>
    
  • A Condition rule checks a Source for a condition, and then applies an action to a Target if the condition evaluates to true.

    In the following example, a target text label indicates when a source value is greater than or less than 4 by comparing Value against the number 4 and updating the text label accordingly:

    <Rules>
        <Condition Source="[Value]" ConditionOp="GreaterThan" SourceValue="4" 
            Target="[Label.Content]" Value="GREATER than 4." />
        <Condition Source="[Value]" ConditionOp="LessThan" SourceValue="4" 
            Target="[Label.Content]" Value="LESS than 4." />
    </Rules>
    
  • A Changed rule applies an action if a Source has changed. A condition or value is not specified.

    In the following example, when the Background color changes, the text label is updated:

    <Rules>
        <Changed Source="[Background.Content]">
            <Actions>
                <Set Target="[Label.Content]" Value="The color changed!" />
            </Actions>
        </Changed>
    </Rules>
    

Sample Explorer

  • Rules > all samples

See Also