首页 »DotNet » wpf事件:c#中的自定义事件在wpf中的具体应用 »正文
wpf事件:c#中的自定义事件在wpf中的具体应用
来源: 发布时间:星期四, 2009年1月15日 浏览:27次 评论:0
最近公司在上 个wpf项目,熟悉WPF 同学都知道,WPFControl控件中,"用户Control控件"这个概念非常常见,我们也经常要做 些用Control控件来实现 些相对比较复杂 功能,比如: 个 2维 仓库管理系统,仓库中 货架可以做成 个用户Control控件,而货架中 某个货架层,货架层中 某个货格,其实都可以是 个用户Control控件,我们在画具体 某个货架 时候,就可以根据这个货架 实际情况,从据库中读取相关 资料,生成具有几格几层 2维货架图形.由于货架 通过几层用户Control控件来实现 ,有时候我们需要在它们"层次"中传递消息,比如,我 某个货格 信息变动了,需要通知整个货架,甚至是加载这个货架 某个窗口,这时候就可以用c# 定义事件来完成了,从触发事件 某 "层"起,往上抛出事件,父Control控件接收事件,然后接着往上抛, 直到接收这个事件 某"层"做出具体 事件处理. 本人才疏学浅,不当的处还望大虾们多多包含! 首先我们做 个简单 用户Control控件,模拟在最底层触发事件 图形Control控件: 简单用户Control控件 <UserControlx: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> <RectangleFill="Bisque"></Rectangle> </Grid> </UserControl>
Control控件代码 using; using.Collections.Generic; using.Linq; using.Text; using.Windows; using.Windows.Controls; using.Windows.Data; using.Windows.Documents; using.Windows.Input; using.Windows.Media; using.Windows.Media.Imaging; using.Windows.Navigation; using.Windows.Shapes; WpfApplication5 { ///<summary> ///Interactionlogicforuc1.xaml ///</summary> publicpartialuc1:UserControl { publicuc1 { InitializeComponent; } private_name; publicName { get; ; } } publicuc1ClickEventArgs { publicName { get; ; } } } uc1ClickEventArgs 类是 个自定义事件参数类,用来装这个Control控件 些信息,供它 上级容器 . 再下来也是 个用户Control控件,用来装多个上面图形Control控件,比如我们可以把它看成是某个货格,而下面就是 个货架,我采用最 基本 循环来生成几个上图中 用户Control控件: <UserControlx: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-:WpfApplication5" Height="300"Width="600"Loaded="UserControl_Loaded"> <Grid> <Canvasx:Name="pa"></Canvas> </Grid> </UserControl>
此Control控件只包含 个容器canvas,用来装成最底层 用户Control控件uc1, 以下是这个Control控件 代码 : using; using.Collections.Generic; using.Linq; using.Text; using.Windows; using.Windows.Controls; using.Windows.Data; using.Windows.Documents; using.Windows.Input; using.Windows.Media; using.Windows.Media.Imaging; using.Windows.Navigation; using.Windows.Shapes; WpfApplication5 { ///<summary> ///Interactionlogicforwhs_map.xaml ///</summary> /// publicdelegatevoidtestDelegate(objectsender,uc1ClickEventArgse); publicpartialwhs_map:UserControl { publicwhs_map { InitializeComponent; } privateeventtestDelegate_testEvent; publiceventtestDelegatetestEvent { add { _testEventvalue; } remove { _testEvent-=value; } } privatevoidUserControl_Loaded(objectsender,RoutedEventArgse) { left=5; top=1; for(i=0;i<5;i) { uc1uc=uc1; uc.MouseLeftButtonDownMouseButtonEventHandler(mouseDown); uc.Name=i.; pa.Children.Add(uc); Canvas.SetTop(uc,top); Canvas.SetLeft(uc,left); left205; } } publicvoidmouseDown(objectsender,MouseButtonEventArgse) { (senderisuc1) { uc1uc=senderasuc1; uc1ClickEventArgse2=uc1ClickEventArgs; e2.Name=uc.Name; _testEvent(this,e2); } } } }
为了实现事件 层层上抛,首先定义 个委托: publicdelegatevoidtestDelegate(objectsender,uc1ClickEventArgse); 接下来就是事件 申明: publiceventtestDelegatetestEvent { add { _testEventvalue; } remove { _testEvent-=value; } } 点击鼠标左键时抛出事件: (senderisuc1) { uc1uc=senderasuc1; uc1ClickEventArgse2=uc1ClickEventArgs; e2.Name=uc.Name; _testEvent(this,e2); } 最后面就是我们 主页面,里面只包含 个canvas容器,代码如下: privatevoidWindow_Loaded(objectsender,RoutedEventArgse) { whs_mapmap=whs_map; map.testEventtestDelegate(map_testEvent); ca.Children.Add(map); } publicvoidmap_testEvent(objectsender,uc1ClickEventArgse) { MessageBox.Show("hello!"+e.Name.); } 在主窗口中对Control控件 事件做出处理.
相关文章
读者评论
发表评论
|
|