📜  XAML-事件处理

📅  最后修改于: 2020-11-18 10:32:09             🧑  作者: Mango


XAML中事件的一般概念类似于.NET和C++等其他流行编程语言中的事件。在XAML中,所有控件都公开一些事件,以便可以出于特定目的订阅它们。

每当发生事件时,都会通知应用程序,并且程序可以对它们做出反应,例如,使用关闭按钮来关闭对话框。

根据应用程序的需求,可以为应用程序的不同行为订阅许多类型的事件,但是最常用的事件是与鼠标和键盘相关的事件,例如,

  • 请点击
  • 鼠标向下
  • 鼠标输入
  • 鼠标离开
  • 滑鼠
  • 按键
  • 按键

在本章中,我们将使用一些基本的和最常用的事件来理解如何将特定控件的事件链接到后面的代码上,该代码将根据用户在特定事件发生时想要执行的操作来实现行为。发生。

让我们看一下按钮单击事件的简单示例。下面给出的是Button控件的XAML实现,它使用一些属性和Click事件(Click =“ OnClick”)创建和初始化。

 
    
    
       
   
   
 

只要单击此按钮,就会触发OnClick事件,您可以添加任何类型的行为作为对Click的响应。让我们看一下OnClick事件实现,单击该按钮时将显示一条消息。

using System; 
using System.Windows; 
using System.Windows.Controls;  

namespace XAMLEventHandling {
   ///  
      /// Interaction logic for MainWindow.xaml 
   ///  
    
   public partial class MainWindow : Window {
      public MainWindow() { 
         InitializeComponent(); 
      }
      private void OnClick(object sender, RoutedEventArgs e) { 
         MessageBox.Show("Button is clicked!"); 
      } 
   }
}

当您编译并执行上述代码时,将产生以下输出-

事件处理

当您单击按钮时,将触发click(OnClick)事件,并显示以下消息。

事件处理功能

现在,让我们看一个处理多个事件的复杂示例。

下面的示例包含一个带有ContextMenu的文本框,该菜单可操纵该文本框中的文本。

下面的XAML代码使用一些属性和事件(例如Checked,Unchecked和Click)创建一个TextBox,一个ContextMenu和MenuItems。

 
    
    
       
         Hi, this is XAML tutorial. 
         
         
            
                
                
                
               
                
             
                
         
      
    
    
 

这是C#中针对不同事件的实现,每当选中,取消选中或单击菜单项时,都会触发该事件。

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;  

namespace XAMLContextMenu { 
   ///  
      /// Interaction logic for MainWindow.xaml 
   /// 
    
   public partial class MainWindow : Window {
      public MainWindow() { 
         InitializeComponent(); 
      }
      private void Bold_Checked(object sender, RoutedEventArgs e) { 
         textBox1.FontWeight = FontWeights.Bold; 
      }
      private void Bold_Unchecked(object sender, RoutedEventArgs e) { 
         textBox1.FontWeight = FontWeights.Normal; 
      }
      private void Italic_Checked(object sender, RoutedEventArgs e) { 
         textBox1.FontStyle = FontStyles.Italic; 
      }
      private void Italic_Unchecked(object sender, RoutedEventArgs e) { 
         textBox1.FontStyle = FontStyles.Normal; 
      }
      private void IncreaseFont_Click(object sender, RoutedEventArgs e) { 
         if (textBox1.FontSize < 18) { 
            textBox1.FontSize += 2; 
         } 
      }
      private void DecreaseFont_Click(object sender, RoutedEventArgs e) { 
         if (textBox1.FontSize > 10) { 
            textBox1.FontSize -= 2; 
         } 
      }
   }
}

当您编译并执行上述代码时,将产生以下输出-

ContextMenu输出

我们建议您执行上述示例代码并尝试其他事件。

大事记

Sr.No. Controls & Description
1

Checked

Fires when a ToggleButton is checked. (Inherited from ToggleButton)

2

Click

Occurs when a button control is clicked. (Inherited from ButtonBase)

3

ContextMenuClosing

Occurs just before any context menu on the element is closed. (Inherited from FrameworkElement.)

4

ContextMenuOpening

Occurs when any context menu on the element is opened. (Inherited from FrameworkElement.)

5

DataContextChanged

Occurs when the value of the FrameworkElement.DataContext property changes. (Inherited from FrameworkElement)

6

DragEnter

Occurs when the input system reports an underlying drag event with this element as the target. (Inherited from UIElement).

7

DragLeave

Occurs when the input system reports an underlying drag event with this element as the origin. (Inherited from UIElement)

8

DragOver

Occurs when the input system reports an underlying drag event with this element as the potential drop target. (Inherited from UIElement)

9

DragStarting

Occurs when a drag operation is initiated. (Inherited from UIElement)

10

DropCompleted

Occurs when a drag-and-drop operation is ended. (Inherited from UIElement)

11

DropDownClosed

Occurs when the drop-down portion of the ComboBox closes.

12

DropDownOpened

Occurs when the drop-down portion of the ComboBox opens.

13

GotFocus

Occurs when a UIElement receives focus. (Inherited from UIElement)

14

Holding

Occurs when an otherwise unhandled Hold interaction occurs over the hit test area of this element. (Inherited from UIElement)

15

Intermediate

Fires when the state of a ToggleButton is switched to the indeterminate state. (Inherited from ToggleButton)

16

IsEnabledChanged

Occurs when the IsEnabled property changes. (Inherited from Control)

17

KeyDown

Occurs when a keyboard key is pressed while the UIElement has focus. (Inherited from UIElement)

18

KeyUp

Occurs when a keyboard key is released while the UIElement has focus. (Inherited from UIElement)

19

LostFocus

Occurs when a UIElement loses focus. (Inherited from UIElement)

20

ManipulationCompleted

Occurs when a manipulation on the UIElement is complete. (Inherited from UIElement)

21

ManipulationDelta

Occurs when the input device changes position during a manipulation. (Inherited from UIElement)

22

ManipulationInertiaStarting

Occurs when the input device loses contact with the UIElement object during a manipulation and inertia begins. (Inherited from UIElement)

23

ManipulationStarted

Occurs when an input device begins a manipulation on the UIElement. (Inherited from UIElement)

24

ManipulationStarting

Occurs when the manipulation processor is first created. (Inherited from UIElement)

25

SelectionChanged

Occurs when the text selection has changed.

26

SizeChanged

Occurs when either the ActualHeight or the ActualWidth property changes value on a FrameworkElement. (Inherited from FrameworkElement)

27

Unchecked

Occurs when a ToggleButton is unchecked. (Inherited from ToggleButton)

28

ValueChanged

Occurs when the range value changes. (Inherited from RangeBase)