C# 为什么WPF ListBox.ListItems不提升MouseRightButtonDown

C# 为什么WPF ListBox.ListItems不提升MouseRightButtonDown,c#,wpf,xaml,routed-events,C#,Wpf,Xaml,Routed Events,我正在试用WPF4发行版中的RoutedEvent示例。该示例在窗口上有许多控件,并在窗口上定义了鼠标右键向下以在窗口的标题栏中显示事件源。 因此,当我们右键单击窗口的任何子元素时,其名称、类型等将显示在窗口的标题栏中。但是,当我右键单击ListBox的ListItem时,它不会在窗口的标题栏中设置其名称、标题等。为什么? 代码如下: XAML <Window x:Class="TempWPFProj.RoutedEventDemo" xmlns="http://schem

我正在试用WPF4发行版中的
RoutedEvent
示例。该示例在
窗口
上有许多控件,并在窗口上定义了
鼠标右键向下
以在窗口的标题栏中显示事件源。 因此,当我们右键单击窗口的任何子元素时,其名称、类型等将显示在窗口的标题栏中。但是,当我右键单击ListBox的ListItem时,它不会在窗口的标题栏中设置其名称、标题等。为什么?

代码如下:

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>

wpf4释放
©2010 SAMS出版公司
已安装章节:
第一章
第二章
帮助
好啊
您已成功注册此产品。
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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
名称空间TempWPFProj
{
/// 
///RoutedEventDemo.xaml的交互逻辑
/// 
公共部分类RoutedEventDemo:窗口
{
公共路由EventDemo()
{
初始化组件();
}
私有无效窗口\u MouseRightButtonDown(对象发送器,MouseButtonEventArgs e)
{
//显示有关此事件的信息
this.Title=“Source=“+e.Source.GetType().Name+”,OriginalSource=”+
e、 OriginalSource.GetType().Name+“@”+e.时间戳;
//在本例中,所有可能的源都来自控件
控制源=e.作为控制源的源;
//切换源代码管理上的边框
if(source.BorderThickness!=新厚度(5))
{
source.BorderThickness=新厚度(5);
source.BorderBrush=画笔.黑色;
}
其他的
source.BorderThickness=新厚度(0);
}
}
}

它在书中正确地说明了为什么列表框不适用于右键单击

“当您右键单击任一ListBoxItem时,Windows从不接收MouseRightButtonDown事件。这是因为ListBoxItem在内部处理此事件以及MouseLeftButtonDown事件(停止冒泡)以实现项目选择”


你的意思是
ListBoxItem
set
e.Handled=true在其事件处理程序中停止冒泡?请尝试
PreviewMouseRightButtonDown