C#定義事件應(yīng)用
C#定義事件應(yīng)用
最近公司在上一個wpf項目,熟悉WPF的同學(xué)都知道,WPF控件中,"用戶控件"這個概念非常常見,我們也經(jīng)常要做一些用控件來實現(xiàn)一些相對比較復(fù)雜的功能,比如:一個二維的倉庫管理系統(tǒng),倉庫中的貨架可以做成一個用戶控件,而貨架中的某個貨架層,貨架層中的某個貨格,其實都可以是一個用戶控件, 我們在畫具體的某個貨架的時候,就可以根據(jù)這個貨架的實際情況,從據(jù)庫中讀取相關(guān)的資料,生成具有幾格幾層的二維貨架圖形.由于貨架的通過幾層用戶控件來實現(xiàn)的,有時候我們需要在它們"層次"中傳遞消息,比如,我的某個貨格的信息變動了,需要通知整個貨架,甚至是加載這個貨架的某個窗口,這時候就可以C#定義事件應(yīng)用來完成了,從觸發(fā)事件的某一"層"起,往上拋出事件,父控件接收事件,然后接著往上拋,一直到接收這個事件的某"層"做出具體的事件處理.
本人才疏學(xué)淺,不當(dāng)之處還望大蝦們多多包含!
首先我們做一個簡單的用戶控件,模擬在***層觸發(fā)事件的圖形控件:
- <UserControlx:ClassUserControlx:Class="WpfApplication5.uc1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Height="60"Width="200">
- <Grid>
- <RectangleFillRectangleFill="Bisque"></Rectangle>
- </Grid>
- </UserControl>
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Windows;
- usingSystem.Windows.Controls;
- usingSystem.Windows.Data;
- usingSystem.Windows.Documents;
- usingSystem.Windows.Input;
- usingSystem.Windows.Media;
- usingSystem.Windows.Media.Imaging;
- usingSystem.Windows.Navigation;
- usingSystem.Windows.Shapes;
- namespaceWpfApplication5
- {
- ///<summary>
- ///Interactionlogicforuc1.xaml
- ///</summary>
- publicpartialclassuc1:UserControl
- {
- publicuc1()
- {
- InitializeComponent();
- }
- privatestring_name;
- publicstringName
- {
- get;
- set;
- }
- }
- publicclassuc1ClickEventArgs
- {
- publicstringName
- {
- get;
- set;
- }
- }
- }
uc1ClickEventArgs 類是一個自定義事件參數(shù)類,用來裝這個控件的一些信息,供它的上級容器調(diào)用.
再下來也是一個用戶控件,用來裝多個上面圖形控件,比如我們可以把它看成是某個貨格,而下面就是一個貨架,我采用最基本的循環(huán)來生成幾個上圖中的用戶控件:
- <UserControlx:ClassUserControlx:Class="WpfApplication5.whs_map"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfApplication5"
- Height="300"Width="600"Loaded="UserControl_Loaded">
- <Grid>
- <Canvasx:NameCanvasx:Name="pa"></Canvas>
- </Grid>
- </UserControl>
- Code
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Windows;
- usingSystem.Windows.Controls;
- usingSystem.Windows.Data;
- usingSystem.Windows.Documents;
- usingSystem.Windows.Input;
- usingSystem.Windows.Media;
- usingSystem.Windows.Media.Imaging;
- usingSystem.Windows.Navigation;
- usingSystem.Windows.Shapes;
- namespaceWpfApplication5
- {
- ///<summary>
- ///Interactionlogicforwhs_map.xaml
- ///</summary>
- ///
- publicdelegatevoidtestDelegate(objectsender,uc1ClickEventArgse);
- publicpartialclasswhs_map:UserControl
- {
- publicwhs_map()
- {
- InitializeComponent();
- }
- privateeventtestDelegate_testEvent;
- publiceventtestDelegatetestEvent
- {
- add
- {
- _testEvent+=value;
- }
- remove
- {
- _testEvent-=value;
- }
- }
- privatevoidUserControl_Loaded(objectsender,RoutedEventArgse)
- {
- intleft=5;
- inttop=1;
- for(inti=0;i<5;i++)
- {
- uc1uc=newuc1();
- uc.MouseLeftButtonDown+=newMouseButtonEventHandler(mouseDown);
- uc.Name=i.ToString();
- pa.Children.Add(uc);
- Canvas.SetTop(uc,top);
- Canvas.SetLeft(uc,left);
- left+=205;
- }
- }
- publicvoidmouseDown(objectsender,MouseButtonEventArgse)
- {
- if(senderisuc1)
- {
- uc1uc=senderasuc1;
- uc1ClickEventArgse2=newuc1ClickEventArgs();
- e2.Name=uc.Name;
- _testEvent(this,e2);
- }
- }
- }
- }
以上介紹C#定義事件應(yīng)用
【編輯推薦】