WPF之路由事件

路由规则之冒泡规则:

<Window x:Class="RoutedEvents.BubbledLabelClick"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="BubbledLabelClick" Height="359" Width="329"
    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" Background="AliceBlue" BorderBrush="Black" BorderThickness="1" MouseUp="SomethingClicked" HorizontalAlignment="Left" >
        <StackPanel MouseUp="SomethingClicked" >
          <TextBlock Margin="3" MouseUp="SomethingClicked" >
            Image and picture label</TextBlock>
          <Image Source="happyface.jpg" Stretch="None" 
                 MouseUp="SomethingClicked" />
          <TextBlock Margin="3" 
                     MouseUp="SomethingClicked" >
            Courtesy of the StackPanel</TextBlock>
        </StackPanel>
      </Label>

      
      <ListBox Margin="5" Name="lstMessages" Grid.Row="1"></ListBox>
      <CheckBox Margin="5" Grid.Row="2" Name="chkHandle">Handle first event</CheckBox>
      <Button Click="cmdClear_Click" Grid.Row="3" HorizontalAlignment="Right" Margin="5" Padding="3">Clear List</Button>
    </Grid>
</Window>

    public partial class BubbledLabelClick : System.Windows.Window
    {

        public BubbledLabelClick()
        {
            InitializeComponent();
        }

        protected int eventCounter = 0;

        private void SomethingClicked(object sender, RoutedEventArgs e)
        {            
            eventCounter++;
            string message = "#" + eventCounter.ToString() + ":\r\n" + 
                " Sender: " + sender.ToString() + "\r\n" +
                " Source: " + e.Source + "\r\n" +
                " Original Source: " + e.OriginalSource;
            lstMessages.Items.Add(message);
            e.Handled = (bool)chkHandle.IsChecked;            
        }

        private void cmdClear_Click(object sender, RoutedEventArgs e)
        {            
            eventCounter = 0;
            lstMessages.Items.Clear();
        }
    }

路由规则之遂道规则:

<Window x:Class="RoutedEvents.TunneledKeyPress"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="TunneledKeyPress" Height="411" Width="403"
     PreviewKeyDown="SomeKeyPressed" 
    >
  <Grid Margin="3" >
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="*"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>

    <Label Margin="5" Background="AliceBlue" BorderBrush="Black" BorderThickness="1" HorizontalContentAlignment="Stretch"
           PreviewKeyDown="SomeKeyPressed">
      <StackPanel
        PreviewKeyDown="SomeKeyPressed">
        <TextBlock Margin="3" HorizontalAlignment="Center"
                   PreviewKeyDown="SomeKeyPressed">
          Image and text label
        </TextBlock>
        <Image Source="happyface.jpg" Stretch="None" 
                PreviewKeyDown="SomeKeyPressed"/>
        <DockPanel Margin="0,5,0,0" PreviewKeyDown="SomeKeyPressed">
          <TextBlock Margin="3" 
                     PreviewKeyDown="SomeKeyPressed">
          Type here:
        </TextBlock>
          <TextBox PreviewKeyDown="SomeKeyPressed" KeyDown="SomeKeyPressed"></TextBox>
        </DockPanel>
      </StackPanel>
    </Label>

    <ListBox Margin="5" Name="lstMessages" Grid.Row="1"></ListBox>
    <CheckBox Margin="5" Grid.Row="2" Name="chkHandle">Handle first event</CheckBox>
    <Button Click="cmdClear_Click" Grid.Row="3" HorizontalAlignment="Right" Margin="5" Padding="3">Clear List</Button>
  </Grid>
</Window>
        protected int eventCounter = 0;

        private void SomeKeyPressed(object sender, RoutedEventArgs e)
        {
            eventCounter++;
            string message = "#" + eventCounter.ToString() + ":\r\n" +
                " Sender: " + sender.ToString() + "\r\n" +
                " Source: " + e.Source + "\r\n" +
                " Original Source: " + e.OriginalSource + "\r\n" +
                " Event: " + e.RoutedEvent;
            lstMessages.Items.Add(message);
            e.Handled = (bool)chkHandle.IsChecked;
        }

        private void cmdClear_Click(object sender, RoutedEventArgs e)
        {
            eventCounter = 0;
            lstMessages.Items.Clear();
        }
    }

路由规则之直接路由:

    <Grid Margin="5">
      <Button Name="cmd" Click="ButtonClick" MouseUp="NeverCalled">Click me.</Button>
    </Grid>
        public ButtonMouseUpEvent()
        {
            InitializeComponent();
            cmd.AddHandler(Button.MouseUpEvent, new RoutedEventHandler(Backdoor), true);
        }
        
        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("The Button.Click event occurred. This may have been triggered with the keyboard.");
        }

        private void NeverCalled(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("You didn't see this message. That would be impossible.");
        }

        private void Backdoor(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("The (handled) Button.MouseUp event occurred.");
        }
    }







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值