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
  • 相关阅读:
    企业项目化管理介绍
    企业IT架构介绍
    移动端运维体系建设
    学习型组织与企业
    css垂直居中怎么设置?文字上下居中和图片垂直居中
    情人节看看他们是怎么用微信红包示爱的
    2017年腾讯首次大规模扫号展开
    随机森林入门攻略(内含R、Python代码)
    11 Facts about Data Science that you must know
    机器学习编程语言之争,Python夺魁
  • 原文地址:https://www.cnblogs.com/shawnzxx/p/3342556.html
Copyright © 2011-2022 走看看