Understanding of WPF routed events

Many explain routed events on the blog garden articles, one of them in turn for this study reference:

 https://www.cnblogs.com/zhili/p/WPFRouteEvent.html

Articles have circulated on the Internet Bubble been described, but they are thousand times, then how can we use a bubbling routed event in actual use it?

 <Grid x:Name="GridRoot" Background="Lime" Button.Click="Button_Click">
        <Grid x:Name="GridA" Margin="10" Background="Blue" Button.Click="Button_Click">
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Canvas x:Name="CanvasLeft" Grid.Column="0" Background="Red" Margin="10" >
                <Button x:Name="ButtonLeft" Width="65" Height="100" Margin="10" Content="Left" Button.Click="Button_Click"></Button>
            </Canvas>
            <Canvas x:Name="CanvasRight" Grid.Column="1" Background="Yellow" Margin="10" Button.Click="Button_Click">
                <Button x:Name="ButtonRight" Width="65" Height="100" Margin="10" Content="Right" Button.Click="Button_Click"></Button>
            </Canvas>
        </Grid>

As shown in the code:

1. bubbling event is "inside out" from the transfer, but not every node is required to have the event. Canvas As nodes are not Button.Click = "Button_Click";

2. Each node is not necessarily the same event, for example Button.Click Grid node = "Button_Click" can be changed Button.Click = "Button_Click0000", the event handler Button_Click0000 processing logic may be different from the Button_Click;

3. bubbling routed event scenarios Application one: If you need to define yourself a new control type AA, AA template contains a lable of control, this time AA does not "trigger" click event, but there lable click event, then you can click on the lable of the event as an additional event to AA, AA controls when clicked, then will be able to "trigger" the event. (Route event is actually carried out by the control node "listen", "listening" to the event to event handler will be executed appearance, not "shock hair," said here is just a trigger easy to understand)

Consider the following code:

Use a AA VS custom controls, the following is the default code generated:

   public class AA : Control
    {
        static AA()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AA), new FrameworkPropertyMetadata(typeof(AA)));
        }
    }
 1   <Style TargetType="{x:Type local:AA}">
 2         <Setter Property="Template">
 3             <Setter.Value>
 4                 <ControlTemplate TargetType="{x:Type local:AA}">
 5                     <Border Background="{TemplateBinding Background}"
 6                             BorderBrush="{TemplateBinding BorderBrush}"
 7                             BorderThickness="{TemplateBinding BorderThickness}">
 8                         <Label Content="123" Background="{TemplateBinding Background}"></Label>
 9                     </Border>
10                 </ControlTemplate>
11             </Setter.Value>
12         </Setter>
13     </Style>

In addition to their line of code plus 8, and the other code is automatically generated. AA controls inside customizable nothing, and I put a lable on the inside. Here is the lable of AA mouseDown events to go, so when you click AA, AA background color change.

<Grid x:Name="GridRoot" Background="Lime" Button.Click="Button_Click1">
        <local:AA x:Name="aa" Label.MouseDown="Button_Click" Background="Green">
        </local:AA>
    </Grid>
 private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.aa.Background = Brushes.Red;
        }

When you click AA background will turn from green to red.

This is very simple code, recorded one of the scenarios bubbling event.

 

Guess you like

Origin www.cnblogs.com/qcst123/p/11967573.html