I am trying out RoutedEvent example from WPF 4 Unleashed book. The example has many controls on a Window and it has defined MouseRightButtonDown on Window to display event source in the title bar of the window. So when we right click on any of the child element of the Window, its name, type etc. is shown in the title bar of the window. However when I right click on ListBox's ListItem, it doesn't set its name, title etc. in the title bar of the Window. Why?

The code is as follows:

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>

Code Behind CS File

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);
        }
    }
}
有帮助吗?

解决方案

It says right in the book why the listbox does not work for right click

"Window never receives the MouseRightButtonDown event when you right-click on either ListBoxItem. That's because ListBoxItem internally handles this event as well as the MouseLeftButtonDown event (halting the bubbling) to implement item selection"

Link to page here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top