Routing events, mouse and keyboard input

Keywords: WPF

  Routing events

Routing events use one of the following three routing strategies:

  • Buoyancy (bubbling):   Call the event handler on the event source. Routing events are then routed to subsequent parent elements until they reach the root of the element tree. Most routing events use a floating routing strategy. Floating route events are often used to report input or state changes from different controls or other UI elements.

  • Direct:   Only the source element itself has the opportunity to call the handler to respond. This is similar to the Windows routing form for events ". However, unlike standard CLR events, direct routing events support class handling, which is described in the upcoming section, and can be used   EventSetter EventTrigger .

  • Tunnel:   Initially, the event handler at the root of the element tree will be called. Then, the routing event will move towards the source node element of the routing event (that is, the element that raises the routing event) Direction, which propagates along the routing line to subsequent child elements. Tunnel routing events are usually used or processed in the process of composing controls. In this way, events in composite parts can be intentionally prohibited or replaced with events specific to the whole control. Input events provided in WPF are usually implemented in tunnel / float pairs. Tunnel events are sometimes referred to as Preview the event, which is determined by the naming convention used by the pair.

 

Generally, bubbles and tunnels appear in pairs. Tunnels are easy to recognize and are prefixed with Preview.

example:  

TunnelLabelClick

BubbledLabelClick

keyboard entry:

Focus: Focusable   = true/false     Space elements are true by default, and non controls are false by default

Control the order of focus, TabIndex

<Window x:Class="Demo.WPFLearning.Keyboard"
        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"
        xmlns:local="clr-namespace:Demo.WPFLearning"
        mc:Ignorable="d"
        Title="Keyboard" Height="450" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <DockPanel Margin="5">
            <TextBlock Margin="3"> Type Here</TextBlock>
            <TextBox Name="Textboxx" PreviewKeyDown="Textboxx_KeyEvent" KeyDown="Textboxx_KeyEvent" PreviewTextInput="Textboxx_TextInput"
                     PreviewKeyUp="Textboxx_KeyEvent" KeyUp="Textboxx_KeyEvent" TextChanged="Textboxx_TextChanged"></TextBox>
        </DockPanel>
        <ListBox Margin="5" Name="listMessage" Grid.Row="1"/>
        <Button Grid.Row="2" HorizontalAlignment="Right" Margin="5" Padding="3" Name="clear" Click="Clear_Click">Clear List</Button>
    </Grid>
</Window>
 public partial class Keyboard : Window
    {
        public Keyboard()
        {
            InitializeComponent();
        }
        public void Textboxx_KeyEvent(object sender,KeyEventArgs e)
        {
            string msg = "Event:" + e.RoutedEvent + "   Key:" + e.Key;
            this.listMessage.Items.Add(msg);
        }
        public void Textboxx_TextInput(object sender, TextCompositionEventArgs e)
        {
            string msg = "Event:" + e.RoutedEvent + "   Key:" + e.Text;
            this.listMessage.Items.Add(msg);
        }

        private void Textboxx_TextChanged(object sender, TextChangedEventArgs e)
        {
            string msg = "Event:" + e.RoutedEvent ;
            this.listMessage.Items.Add(msg);
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            this.listMessage.Items.Clear();
        }
    }

 

Mouse input:

Move event MousePosition:

MouseEventArgs object: contains some information that identifies the state of the mouse

Direct event: MouseEnter   , MouseLeave

Tunnel routing events: PreviewMouseMove, MouseMove

    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Rectangle x:Name="rect" Fill="AliceBlue" MouseMove="Rect_MouseMove"/>
        <Button Grid.Row="1" Name="cmdCapture"> Capture the Mouse</Button>
        <TextBlock x:Name="Info" Grid.Row="2"/>
    </Grid>
        private void Rect_MouseMove(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            this.Info.Text = ("Mouse is at ( "+pt.X +" , "+pt.Y+" ) in window" );
        }

 

Click event:

Mouse.Capture(), the first parameter is mouse, the second parameter is type, optional

When a mouse event is captured by an element, other elements cannot receive the mouse event, so you need to notify the mouse button release event (the mouse leaves the original element)

Routing events:

PreviewMouseLeftButtonDown,MouseRightButtonDown,LeftButtonDown,RightButtonDown

PreviewMouseLeftButtonUp,MouseRightButtonUp,LeftButtonUp,RightButtonUp

 

Mouse drag and drop   MouseDragAndDrop

  1. After clicking or selecting some elements, some information is shelved.
  2. When the user moves the mouse over other elements, the elements can accept the dragged content, and the mouse pointer changes to a drag and drop icon
  3. When the mouse button is released, the target element receives and processes the information

  Some controls, such as TextBox's own drag and drop logic, write some code for label.

Dragdrop.dodragdrop (source, attribute content, DragDropEffects.Copy (mode));

For the element receiving content, the attribute AllowDrop should be modified to true

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBox Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center" Text="Drag from this TextBox"/>
        <TextBox Padding="10" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="1"/>
        <Label x:Name="LabFrom"  Padding="20" VerticalAlignment="Center" HorizontalAlignment="Center" 
               Grid.Row="1" Background="#FFC4F1A3" MouseDown="LabFrom_MouseDown">or this Label</Label>
        <Label x:Name="LabTo"  Padding="20" VerticalAlignment="Center" HorizontalAlignment="Center" 
               Grid.Column="1" Grid.Row="1"  Background="#FFC4F1A3" AllowDrop="True" Drop="LabTo_Drop">To this Label</Label>

    </Grid>
 private void LabFrom_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            DragDrop.DoDragDrop(label,Content,DragDropEffects.Copy);
        }

        private void LabTo_Drop(object sender, DragEventArgs e)
        {
            Label label = (Label)sender;
            label.Content = e.Data.GetData(DataFormats.Text);
        }

 

Posted by theda on Fri, 03 Dec 2021 19:16:36 -0800