How can I provide multiple conditions for data trigger in WPF?

后端 未结 4 1795
臣服心动
臣服心动 2020-12-02 05:04

How can I provide multiple conditions for data trigger in WPF?

4条回答
  •  情话喂你
    2020-12-02 05:39

    THIS ANSWER IS FOR ANIMATIONS ONLY


    If you wanna implement the AND logic, you should use MultiTrigger, here is an example:

    Suppose we want to do some actions if the property Text="" (empty string) AND IsKeyboardFocused="False", then your code should look like the following:

    
        
            
            
        
            
                
            
    
    

    If you wanna implement the OR logic, there are couple of ways, and it depends on what you try to do:

    The first option is to use multiple Triggers.
    So, suppose you wanna do something if either Text="" OR IsKeyboardFocused="False",
    then your code should look something like this:

    
        
    
    
        
    
    


    But the problem in this is what will I do if i wanna do something if either Text ISN'T null OR IsKeyboard="True"? This can be achieved by the second approach:
    Recall De Morgan's rule, that says !(!x && !y) = x || y.
    So we'll use it to solve the previous problem, by writing a multi trigger that it's triggered when Text="" and IsKeyboard="True", and we'll do our actions in EXIT ACTIONS, like this:

    
        
            
            
        
        
            
        
    
    

提交回复
热议问题