use of boolean to color converter in XAML

后端 未结 3 641
甜味超标
甜味超标 2020-12-05 23:25

I am working on WPF application.I have bound my textblock to my button. I want to set foreground of my textblock to black color when its associated button\'s isEnabled is tr

3条回答
  •  爱一瞬间的悲伤
    2020-12-05 23:47

    The answer above shows you how to correctly you use a converter. However, do you really need to use a converter? This can be done in XAML only using Triggers:

    XAML

            
    
                
    
                
    
                
    
            
    

    In the example above, the first TextBlock binds to its parent's IsEnabled property using a DataTrigger, and sets the Foreground to some colour if it is true.

    However, this is overkill - the IsEnabled property is propagated down to children automatically by WPF. That is, if you set IsEnabled to false on your Button, then your TextBlock will have its IsEnabled property updated to false automatically. This is demonstrated in the second TextBlock which uses a property Trigger to check its own IsEnabled property against the value of true (since its IsEnabled property will be the same as its parent's). This would be the preferred approach.

    Hope this helps!

提交回复
热议问题