2014-04-21 3 views
-1

WPF 4 Unleashed 책에서 예제를 RoutedEvent 시도하고 있습니다. 이 예제는 Window에 많은 컨트롤을 가지고 있으며 윈도우의 제목 표시 줄에 이벤트 소스를 표시하기 위해 Window에 MouseRightButtonDown을 정의했습니다. 그래서 Window의 자식 요소 중 하나를 마우스 오른쪽 버튼으로 클릭하면 이름, 유형 등이 창의 제목 표시 줄에 표시됩니다. 그러나 ListBox의 ListItem을 마우스 오른쪽 버튼으로 클릭하면 이름, 제목 등이 제목 표시 줄에 설정되지 않습니다. 왜? 다음과 같이WPF ListBox.ListItems가 MouseRightButtonDown을 발생시키지 않는 이유

코드는 다음과 같습니다

XAML

<Window x:Class="TempWPFProj.RoutedEventDemo" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="About WPF 4 Unleashed" 
     SizeToContent="WidthAndHeight" 
     Background="OrangeRed" MouseRightButtonDown="Window_MouseRightButtonDown"> 
    <StackPanel> 
     <Label FontWeight="Bold" FontSize="20" Foreground="White"> 
      WPF 4 Unleashed 
     </Label> 
     <Label>© 2010 SAMS Publishing</Label> 
     <Label>Installed Chapters:</Label> 
     <ListBox> 
      <ListBoxItem>Chapter 1</ListBoxItem> 
      <ListBoxItem>Chapter 2</ListBoxItem> 
     </ListBox> 
     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 
      <Button MinWidth="75" Margin="10">Help</Button> 
      <Button MinWidth="75" Margin="10">OK</Button> 
     </StackPanel> 
     <StatusBar>You have successfully registered this product.</StatusBar> 
    </StackPanel> 
</Window> 

코드 CS 파일 뒤에

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
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.Shapes; 

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

     private void Window_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
     { 
      // Display information about this event 
      this.Title = "Source = " + e.Source.GetType().Name + ", OriginalSource = " + 
      e.OriginalSource.GetType().Name + " @ " + e.Timestamp; 
      // In this example, all possible sources derive from Control 
      Control source = e.Source as Control; 
      // Toggle the border on the source control 
      if (source.BorderThickness != new Thickness(5)) 
      { 
       source.BorderThickness = new Thickness(5); 
       source.BorderBrush = Brushes.Black; 
      } 
      else 
       source.BorderThickness = new Thickness(0); 
     } 
    } 
} 
+0

시도'PreviewMouseRightButtonDown'을 처리하기 때문이다 –

답변

1

그것은 바로 목록 상자가 작동하지 않는 이유를이 책에서 말한다 오른쪽 클릭

"ListBoxItem 중 하나를 마우스 오른쪽 단추로 클릭하면 Window가 MouseRightButtonDown 이벤트를 수신하지 않습니다. ListBoxItem의 내부 이벤트뿐만 아니라 (버블 링을 중지)에 MouseLeftButtonDown 이벤트 항목 선택을 구현하는 "

Link to page here

+0

당신은 = 사실 ListBoxItem'는'e.Handled 설정'을 의미한다.; '버블 링을 멈추는 이벤트 핸들러 내부? – Mahesha999