이벤트 라우팅(Event Routing)
.NET프로그래밍/Silverlight 3.0 2009. 12. 1. 16:51 |RoutedEventArgs 클래스
- 라우트된 이벤트란 이벤트를 발생시킨 특정 개체뿐 아니라 요소 트리의 여러 수신기에서 처리기를 호출할 수 있는 이벤트 형식입니다.
이벤트 버블링
- 자식 개체에서 발생된 이벤트가 부모 요소로 전달
[MainPage.xaml]
<UserControl x:Class="RiaRoutedEventArgs.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Rectangle x:Name="rect" Fill="Blue" Margin="20" />
<TextBlock x:Name="lbl" Text="텍스트" FontSize="30" Foreground="White" Width="100" Height="50">
</TextBlock>
</Grid>
</UserControl>
[MainPage.xaml.cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace RiaRoutedEventArgs
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
// 그리드
this.LayoutRoot.MouseLeftButtonUp += new MouseButtonEventHandler(LayoutRoot_MouseLeftButtonUp);
// 사각형
this.rect.MouseLeftButtonUp += new MouseButtonEventHandler(rect_MouseLeftButtonUp);
// 텍스트블록
this.lbl.MouseLeftButtonUp += new MouseButtonEventHandler(lbl_MouseLeftButtonUp);
}
void lbl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("텍스트블록 : 마우스업 이벤트 발생");
//[!] 이벤트 버블링(부모 요소에게 이벤트 전달) 중지 : 라우팅 중지
e.Handled = true;
}
void rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("사각형 : 마우스업 이벤트 발생");
}
void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("그리드 레이아웃 : 마우스업 이벤트 발생");
//[!] 이벤트를 발생시킨 개체 찾기
if (e.OriginalSource is Rectangle)
{
Rectangle r = e.OriginalSource as Rectangle; // 원본 개체를 사각형 개체로 변경
r.Fill = new SolidColorBrush(Colors.Red);
}
}
}
}
[실행결과]
--> 첫 화면.
--> "텍스트"라고 글씨가 쓰여있는 "TextBlock"부분을 마우스로 클릭했을 때 화면.(--> "텍스트블록"부분에는 이벤트 버블링(= 라우팅)을 중지하였다.
--> "TextBlock"바깥쪽 부분인 "Rectangle(사각형 부분)"을 클릭하였을 때의 화면.
--> 바로 위의 그림에서 '메시지박스'의 '확인'버튼을 눌렀을 때의 화면.(Rectangle(사각형)부분은 이벤트 버블링(= 라우팅)이 발생하였다.)
--> 바로 위의 그림에서 '메시지박스'의 '확인'버튼을 눌렀을 때의 화면.
--> 웹 페이지 가장 바깥쪽에 있는, '흰색'의 'Grid'부분을 클릭했을 때의 화면.
'.NET프로그래밍 > Silverlight 3.0' 카테고리의 다른 글
HyperlinkButton 컨트롤 (0) | 2009.12.02 |
---|---|
코드 비하인드에서 붙임(Attached) 속성 사용 (0) | 2009.12.02 |
InkPresenter : 잉크 (0) | 2009.12.01 |
드래그 앤 드롭 (0) | 2009.12.01 |
TextBlock 컨트롤 : 텍스트 출력 (0) | 2009.12.01 |