zoukankan      html  css  js  c++  java
  • Routed Events【pluralsight】

    Routing Strategies:

    • Direct
    • Bubbling
    • Tunneling

    WHy use them?

    • Any UIElement can be a listener
    • Common handlers
    • Visual Tree Communication
    • Event Setter and Event Trigger

    Routed Event Example:

    <Window x:Class="CustomControlDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow"
            Width="350"
            Height="220"
            ButtonBase.Click="Window_Click">   //listen to the RoutedEvent:ButtonBase.Click and handle RoutedEvent using Window_Click
        <Grid>
            <StackPanel Margin="20" ButtonBase.Click="StackPanel_Click">   //listen to the RoutedEvent:ButtonBase.Click and handle RoutedEvent using StackPanel_Click
                <Border BorderBrush="Red" BorderThickness="5">
                    <TextBlock Margin="5"
                               FontSize="18"
                               Text="This is a TextBlock" />
                </Border>
                <Button Margin="10"
                        Click="Button_Click"  //handle the Button.Click event using Button_Click handler
                        Content="Click me" />
            </StackPanel>
        </Grid>
    </Window>
    namespace CustomControlDemo
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
            //Button层的event handler,由于ButtonBase.Click是routedEvent所以可以向上bubble
            }
    
            private void StackPanel_Click(object sender, RoutedEventArgs e)
            {
                
            e.Handled = true;  //StackPanel层的event handler,不继续向上传
                e.Handled = false;  //StackPanel层的event handler,继续向上传
            }
    
            private void Window_Click(object sender, RoutedEventArgs e)
            {
             //Window层的event handler,已传到最上层
            }
        }
    }

    我们也可以不在xaml写handler,直接用后台控制

    namespace CustomControlDemo
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                AddHandler(Button.ClickEvent, new RoutedEventHandler(Window_Click), true);  //即使e.Handled = true;我们也要routedevent继续传到window层
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
            //Button层的event handler,由于ButtonBase.Click是routedEvent所以可以向上bubble
            }
    
            private void StackPanel_Click(object sender, RoutedEventArgs e)
            {
            e.Handled = true;
            }
    
            private void Window_Click(object sender, RoutedEventArgs e)
            {
            
            }
        }
    }    

     Custom Routed Events

    • register your event
    • choose your routing strategy
    • provide add and remove CLR wrapper
    • Raise your event
  • 相关阅读:
    论球迷和程序员
    山哥,你是怎么提高设计能力的?
    一个想休息的线程:JVM到底是怎么处理锁的?怎么不让我阻塞呢?
    由“面经”引发的思考
    99%的创业公司都不值得加入
    大牛是怎么炼成的?
    RMQ问题 与众不同 尚未攻克
    YbtOj例题:二叉堆3 龙珠游戏
    离散化模板
    YbtOJ练习:广搜 3 追捕小狗
  • 原文地址:https://www.cnblogs.com/shawnzxx/p/3342556.html
Copyright © 2011-2022 走看看