views:

73

answers:

3

Hi I'm learnign WPF and currently I'm reading about RoutedEvents. In book "Pro WPF in c#" there is some snipet of code which I present below

<Window x:Class="RoutedEventsLearning.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" MouseUp="SomethingClicked" >
    <Grid Margin="3" MouseUp="SomethingClicked">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Label Margin="5" Grid.Row="0" HorizontalAlignment="Left"
Background="AliceBlue" BorderBrush="Black" BorderThickness="1"
MouseUp="SomethingClicked">
            <StackPanel MouseUp="SomethingClicked">
                <TextBlock Margin="3"
MouseUp="SomethingClicked">
Image and text label</TextBlock>
                <Image  Stretch="None"
MouseUp="SomethingClicked" />
                <TextBlock Margin="3"
MouseUp="SomethingClicked">
Courtesy of the StackPanel</TextBlock>
            </StackPanel>
        </Label>
        <ListBox Grid.Row="1" Margin="5" Name="lstMessages"></ListBox>
        <CheckBox Grid.Row="2" Margin="5" Name="chkHandle">
            Handle first event</CheckBox>

    </Grid>
</Window>

The point is that I see no difference/advantage of routed events (judgin by this example). In regular C# I would do the same thing - add handler to every element on my form (just like this was done in here).

At first when I was reading about routed events it seems that it works that way. I add event handler for parent of all visual elements on my form/control/windows. If I click on child the mousedown event is raised and travels down through visual tree. If event reaches to grid the event handler is invoked. However I was wrong or I did sth wrong. Could somebody explain me difference between normal events and routed events

Edit Something strange is going on with inserting a code. Insted of entire code snipet I get only tag

A: 

Not sure what you mean. RoutedEvents can travel up or down the visual tree based on the RoutingStrategy. By default the RoutingStrategy is set to Bubble (traveling from child to parent).

I tried your code and it seems to behave as expected. Clicking on "Image and text label" it routes up the visual tree: TextBlock -> StackPanel -> Label -> Grid

letstango
To clarify: The default value of RoutingStrategy only affects new RoutedEvents that you define. The events defined on existing controls all define a specific value, in many cases Bubble for the normal event and Tunnel for the associated Preview event.
John Bowen
A: 

The point is that I see no difference/advantage of routed events (judging by this example). In regular C# I would do the same thing - add handler to every element on my form (just like this was done in here).

The point of the example is to show how flagging the event as handled affects routing behavior. If you check the "Handle first event" checkbox and then click on one of the inner controls, the event gets raised only on that control. If you uncheck it, it gets raised on every control in the visual tree.

The only reason that the author of the example handles the event on every object in the tree is to demonstrate this. Normally you wouldn't do this; you'd just handle the event at the highest useful level.

Robert Rossney
A: 

So normally I just should add an event handler to grid, and even if I click other control(which is a children of grid) the event will be routed to grid and fire the handler ?

wpfnewbie