C#定义事件应用

开发 后端
本文介绍C#定义事件应用,从据库中读取相关的资料,生成具有几格几层的二维货架图形.由于货架的通过几层用户控件来实现的。

C#定义事件应用

最近公司在上一个wpf项目,熟悉WPF的同学都知道,WPF控件中,"用户控件"这个概念非常常见,我们也经常要做一些用控件来实现一些相对比较复杂的功能,比如:一个二维的仓库管理系统,仓库中的货架可以做成一个用户控件,而货架中的某个货架层,货架层中的某个货格,其实都可以是一个用户控件, 我们在画具体的某个货架的时候,就可以根据这个货架的实际情况,从据库中读取相关的资料,生成具有几格几层的二维货架图形.由于货架的通过几层用户控件来实现的,有时候我们需要在它们"层次"中传递消息,比如,我的某个货格的信息变动了,需要通知整个货架,甚至是加载这个货架的某个窗口,这时候就可以C#定义事件应用来完成了,从触发事件的某一"层"起,往上抛出事件,父控件接收事件,然后接着往上抛,一直到接收这个事件的某"层"做出具体的事件处理.
本人才疏学浅,不当之处还望大虾们多多包含!

首先我们做一个简单的用户控件,模拟在***层触发事件的图形控件:

  1. <UserControlx:ClassUserControlx:Class="WpfApplication5.uc1" 
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4. Height="60"Width="200"> 
  5. <Grid> 
  6. <RectangleFillRectangleFill="Bisque"></Rectangle> 
  7.  
  8. </Grid> 
  9. </UserControl> 
  10.  
  11. usingSystem;  
  12. usingSystem.Collections.Generic;  
  13. usingSystem.Linq;  
  14. usingSystem.Text;  
  15. usingSystem.Windows;  
  16. usingSystem.Windows.Controls;  
  17. usingSystem.Windows.Data;  
  18. usingSystem.Windows.Documents;  
  19. usingSystem.Windows.Input;  
  20. usingSystem.Windows.Media;  
  21. usingSystem.Windows.Media.Imaging;  
  22. usingSystem.Windows.Navigation;  
  23. usingSystem.Windows.Shapes;  
  24.  
  25. namespaceWpfApplication5  
  26. {  
  27. ///<summary> 
  28. ///Interactionlogicforuc1.xaml  
  29. ///</summary> 
  30. publicpartialclassuc1:UserControl  
  31. {  
  32. publicuc1()  
  33. {  
  34. InitializeComponent();  
  35. }  
  36.  
  37. privatestring_name;  
  38.  
  39. publicstringName  
  40. {  
  41. get;  
  42. set;  
  43. }  
  44. }  
  45. publicclassuc1ClickEventArgs  
  46. {  
  47. publicstringName  
  48. {  
  49. get;  
  50. set;  
  51. }  
  52. }  

uc1ClickEventArgs 类是一个自定义事件参数类,用来装这个控件的一些信息,供它的上级容器调用.

再下来也是一个用户控件,用来装多个上面图形控件,比如我们可以把它看成是某个货格,而下面就是一个货架,我采用最基本的循环来生成几个上图中的用户控件:

  1. <UserControlx:ClassUserControlx:Class="WpfApplication5.whs_map" 
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4. xmlns:local="clr-namespace:WpfApplication5" 
  5. Height="300"Width="600"Loaded="UserControl_Loaded"> 
  6. <Grid> 
  7. <Canvasx:NameCanvasx:Name="pa"></Canvas> 
  8. </Grid> 
  9. </UserControl> 
  10.  
  11. Code  
  12. usingSystem;  
  13. usingSystem.Collections.Generic;  
  14. usingSystem.Linq;  
  15. usingSystem.Text;  
  16. usingSystem.Windows;  
  17. usingSystem.Windows.Controls;  
  18. usingSystem.Windows.Data;  
  19. usingSystem.Windows.Documents;  
  20. usingSystem.Windows.Input;  
  21. usingSystem.Windows.Media;  
  22. usingSystem.Windows.Media.Imaging;  
  23. usingSystem.Windows.Navigation;  
  24. usingSystem.Windows.Shapes;  
  25.  
  26. namespaceWpfApplication5  
  27. {  
  28. ///<summary> 
  29. ///Interactionlogicforwhs_map.xaml  
  30. ///</summary> 
  31. ///  
  32.  
  33. publicdelegatevoidtestDelegate(objectsender,uc1ClickEventArgse);  
  34.  
  35.  
  36. publicpartialclasswhs_map:UserControl  
  37. {  
  38. publicwhs_map()  
  39. {  
  40. InitializeComponent();  
  41. }  
  42.  
  43. privateeventtestDelegate_testEvent;  
  44.  
  45. publiceventtestDelegatetestEvent  
  46. {  
  47. add  
  48. {  
  49. _testEvent+=value;  
  50. }  
  51. remove  
  52. {  
  53. _testEvent-=value;  
  54. }  
  55. }  
  56.  
  57. privatevoidUserControl_Loaded(objectsender,RoutedEventArgse)  
  58. {  
  59. intleft=5;  
  60. inttop=1;  
  61.  
  62. for(inti=0;i<5;i++)  
  63. {  
  64. uc1uc=newuc1();  
  65. uc.MouseLeftButtonDown+=newMouseButtonEventHandler(mouseDown);  
  66.  
  67. uc.Name=i.ToString();  
  68. pa.Children.Add(uc);  
  69.  
  70. Canvas.SetTop(uc,top);  
  71. Canvas.SetLeft(uc,left);  
  72.  
  73. left+=205;  
  74. }  
  75. }  
  76.  
  77. publicvoidmouseDown(objectsender,MouseButtonEventArgse)  
  78. {  
  79. if(senderisuc1)  
  80. {  
  81. uc1uc=senderasuc1;  
  82.  
  83. uc1ClickEventArgse2=newuc1ClickEventArgs();  
  84. e2.Name=uc.Name;  
  85. _testEvent(this,e2);  
  86. }  
  87. }  
  88. }  

以上介绍C#定义事件应用

【编辑推荐】

  1. C#字符串进行分割
  2. 全面测试C#字符串
  3. C# out和ref传递数组
  4. 浅析C#定义整型数组
  5. C#数据库连接字符串
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-04 09:56:46

C#事件处理自定义事件

2009-09-07 04:19:56

C#窗体事件

2009-09-03 15:46:57

C#自定义事件

2009-08-04 12:56:51

C#自定义事件

2009-08-04 12:40:34

c#自定义事件

2009-08-04 13:53:58

C#委托类C#事件

2009-08-04 13:31:35

C#自定义事件

2009-08-31 16:37:20

C#接口定义

2009-08-28 09:43:05

C#事件

2009-08-18 10:48:25

C#事件

2009-08-12 15:20:21

C#事件处理

2009-08-27 18:02:22

C#事件处理

2009-08-28 13:12:02

C# Page_Err

2009-08-13 10:42:31

C#窗体拖动事件

2009-09-07 15:31:49

C#支持事件

2009-08-12 18:28:09

C#事件处理程序

2009-09-10 18:18:42

C# Button

2011-04-25 09:22:44

C#事件

2009-08-28 14:28:25

C# Applicat

2009-08-27 16:53:01

C#委托C#事件
点赞
收藏

51CTO技术栈公众号