Search code examples
wpftriggersicommandcanexecute

WPF UserControl and ICommand


I have created a UserControl which has a button inside and also a style for the button. The style sets the button background upon a mouse over trigger. Now i've added an dependency property of type ICommand and sets the button command to this new property. The command binding works properly however when custom control is disabled (canexecute = false) the mouse over trigger still changes the background.

I've tried changing the mouse over trigger like this but it doesn't work:

<MultiTrigger.Conditions>
    <Condition Property="IsMouseOver" Value="True"/>
    <Condition Property="IsEnabled" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Orange"/>
</MultiTrigger>

Can anyone help?


Solution

  • I think that the IsEnabled property you refer to in the Condition is the property of the Control, not the Button. So the Control is Enabled, and that's why the trigger conditions are true. To access the button you may try using Relative Source Markup Extension.
    You may try something like:

    <Condition Binding="{Binding RelativeSource={RelativeSource
                                     AncestorType={x:Type Button}},
                                 Path=IsEnabled}"
               Value="True"/>