【问题标题】:Why WPF ListBox.ListItems do not raise MouseRightButtonDown为什么 WPF ListBox.ListItems 不会引发 MouseRightButtonDown
【发布时间】:2014-04-21 14:53:01
【问题描述】:

我正在尝试 WPF 4 Unleashed 书中的 RoutedEvent 示例。该示例在Window 上有许多控件,并且它在Window 上定义了MouseRightButtonDown 以在窗口的标题栏中显示事件源。 因此,当我们右键单击 Window 的任何子元素时,其名称、类型等都会显示在窗口的标题栏中。但是,当我右键单击 ListBox 的 ListItem 时,它并没有在 Window 的标题栏中设置其名称、标题等。为什么?

代码如下:

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);
        }
    }
}

【问题讨论】:

  • 试试PreviewMouseRightButtonDown

标签: c# wpf xaml routed-events


【解决方案1】:

它在书中说为什么列表框不能右键单击

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

Link to page here

【讨论】:

  • 您的意思是ListBoxItem 在其事件处理程序中设置e.Handled = true; 以停止冒泡?
猜你喜欢
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 2014-12-11
  • 2011-12-03
  • 2016-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多