专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »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控件事件做出处理.





0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: