簡體   English   中英

需要幫助來了解WPF中的事件冒泡代碼

[英]need help understanding event bubbling code in WPF

以下代碼應該是由於事件冒泡而使單擊按鈕之一時將窗體中存在的所有按鈕變為綠色,但是在我的Visual Studio 2008機器上,它僅將單擊的按鈕變為綠色,可以嗎?幫助找出問題所在?

XAML代碼(window1.xaml):

<Window x:Class="EventRouting.Window1" Title="Event Routing" Height="300" Width="300"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Border Margin="15" BorderBrush="Blue" BorderThickness="5" Padding="15"
            CornerRadius="12" x:Name="myBorder" Background="Transparent">
<StackPanel x:Name="myPanel" Background="Transparent">
  <Ellipse x:Name="myEllipse" Margin="3" Fill="Green" Height="40" />
  <Rectangle x:Name="myRectangle" Margin="3" Fill="Cyan" Height="40" RadiusX="10" RadiusY="10" />
</StackPanel>

CS代碼(window1.xaml.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace EventRouting
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            this.MouseEnter += MouseEnterHandler;
        myBorder.MouseEnter += MouseEnterHandler;
        myPanel.MouseEnter += MouseEnterHandler;
        myEllipse.MouseEnter += MouseEnterHandler;
        myRectangle.MouseEnter += MouseEnterHandler;

        this.MouseDown += MouseDownHandler;
        myBorder.MouseDown += MouseDownHandler;
        myPanel.MouseDown += MouseDownHandler;
        myEllipse.MouseDown += MouseDownHandler;
        myRectangle.MouseDown += MouseDownHandler;

        for (int i = 1; i <= 5; ++i)
        {
            Button btn = new Button();
            btn.Content = "Button " + i;
            myPanel.Children.Add(btn);

            //btn.Click += new RoutedEventHandler(btn_Click);
        }

        myPanel.AddHandler(Button.ClickEvent, new RoutedEventHandler(btn_Click));
    }

    void btn_Click(object sender, RoutedEventArgs e)
    {
        Button btn = (Button) e.Source;
        btn.Background = Brushes.Green;
    }

    void MouseEnterHandler(object sender, MouseEventArgs e)
    {
        Debug.WriteLine("MouseEnter: " + sender);
    }
    void MouseDownHandler(object sender, MouseButtonEventArgs e)
    {
        Debug.WriteLine("MouseDown: " + sender);
        e.Handled = true;
    }

}
}

路由事件使可視化樹冒泡,直到得到處理。 使用您的XAML,嘗試以下代碼。

    public MainWindow()
    {
        InitializeComponent();
        myEllipse.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
        myPanel.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
        myBorder.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(OnMouseDown));
    }

    void OnMouseDown(object sender, RoutedEventArgs e)
    {
        UIElement uiElement = sender as UIElement;
        Debug.WriteLine(uiElement.GetType().ToString());
        e.Handled = true;
    }

如果您注釋掉e.Handled = true行,則該事件將冒泡至父元素。 這是給您的一個很好的鏈接

如果我了解您想要的是一個從根(面板)到每個子節點(按鈕)的隧道事件。 路由隧道事件不這樣做,它們僅從根遍歷到源元素,而不遍歷到兄弟姐妹。 有關詳細說明,請參閱WPF中的路由事件概述

因為事件處理程序btn_Click(對象發送者,MouseEventArgs e)正在發送您單擊的按鈕,所以您不能單獨使用發送者對象將文本更改為綠色。

您應該執行諸如foreach (Button btn in myPanel.Children) ,在其中循環所有按鈕並更改循環中的顏色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM