polfosol polfosol - 1 year ago 75
C# Question

Is it possible that the EventSetter of a Style runs side by side with another event handler?

I have defined a

for a
in WPF. In that style, I want the button's background to be changed when it is clicked. For example:

<Style x:Key="TheButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Margin" Value="5"/>
<EventSetter Event="Click" Handler="ChangeBackground"/>

and in the code behind:

private void ChangeBackground(object sender, RoutedEventArgs e)
var btn = sender as Button;
if(btn != null)
btn.Background = Brushes.SeaShell;

So this a general style that is applied to several
s, each performing a different job. Is it possible that the above event-handler could run along with the other event handler that is specified for each button when they are clicked?

If I specify a
handler for a button that uses this style, does it override the
or the
has the higher priority?

Answer Source

I would use some XAML magic to change the Background (but that's a matter of taste). The Click handler of the button works in parallel.

    <Style TargetType="Button">
            <EventTrigger RoutedEvent="Click">
                        <ColorAnimation To="#FFFF0000" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download