Trigger won't change Background

前端 未结 3 1836
失恋的感觉
失恋的感觉 2021-01-06 15:13

I\'m trying to create a template for buttons in order to prevent the graying of the control when it is disabled, it is working just fine, but for some reason it won\'t chang

3条回答
  •  伪装坚强ぢ
    2021-01-06 15:48

    It wont change the color because you "overlay" the color with the color you defined in MainWindow.xaml.

    Its called dependency property value precedence.

    To read more about it take a look at this:

    http://msdn.microsoft.com/en-us/library/ms743230.aspx

    What happens in your case is that you defined a background in style and in trigger but then you set the value in MainWindow. The value set in MainWindow is considered the local value and the ones set in style are "style - values".

    If you take a look at the link I gave you you will see that local value comes first before "style - values".

    Local values are very powerful.

    That is why your Button will always be Orange even when disabled. The property system will try to find a value for the background when your trigger fires and it will always find the local one because that is the value with the highest prority.

    In order to make this work properly you will have to use SetCurrentValue instead of SetValue when setting the value of Button's background. (Means you will have to override Buttons code)

    Take a look at this:

    http://msdn.microsoft.com/en-us/library/system.windows.dependencyobject.setcurrentvalue.aspx

    This method is used by a component that programmatically sets the value of one of its own properties without disabling an application's declared use of the property. The SetCurrentValue method changes the effective value of the property, but existing triggers, data bindings, and styles will continue to work.

    Edit:

    Example to understand dependency property value precedence and hit testing.

    
        
        
    
    
        
        
            
                
                    
                    
                
            
        
    
    

提交回复
热议问题