VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • c#的自定义属性在WPF中的具体应用

制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
 

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

  本人才疏学浅,不当之处还望大虾们多多包含!

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

  简单用户控件

<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>

 

  控件代码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 类是一个自定义事件参数类,用来装这个控件的一些信息,供它的上级容器调用.

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

  基本的循环来生成几个上图中的用户控件:

<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-namespace:WpfApplication5"
  Height="300"Width="600"Loaded="UserControl_Loaded">
  <Grid>
    <Canvasx:Name="pa"></Canvas>
  </Grid>
</UserControl>

 

  此控件只包含一个容器canvas,用来装成最底层的用户控件uc1,

  以下是这个控件的代码 :

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);
      }
    }
  }
}

 

  为了实现事件的层层上抛,首先定义一个委托:

  publicdelegatevoidtestDelegate(objectsender,uc1ClickEventArgse);

  接下来就是事件的申明:

publiceventtestDelegatetestEvent
    {
      add
      {
        _testEvent+=value;
      }
      remove
      {
        _testEvent-=value;
      }
    }

  点击鼠标左键时抛出事件:

if(senderisuc1)
      {
        uc1uc=senderasuc1;
        uc1ClickEventArgse2=newuc1ClickEventArgs();
        e2.Name=uc.Name;
        _testEvent(this,e2);
      }

  最后面就是我们的主页面,里面只包含一个canvas容器,代码如下:

privatevoidWindow_Loaded(objectsender,RoutedEventArgse)
    {
      whs_mapmap=newwhs_map();
      map.testEvent+=newtestDelegate(map_testEvent);
      ca.Children.Add(map);
    }
    publicvoidmap_testEvent(objectsender,uc1ClickEventArgse)
    {
      MessageBox.Show("hello!"+e.Name.ToString());
    }

  在主窗口中对控件的事件做出处理.

c#中的自定义事件在wpf中的具体应用

 

 

  控件代码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 类是一个自定义事件参数类,用来装这个控件的一些信息,供它的上级容器调用.

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

  基本的循环来生成几个上图中的用户控件:

<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-namespace:WpfApplication5"
  Height="300"Width="600"Loaded="UserControl_Loaded">
  <Grid>
    <Canvasx:Name="pa"></Canvas>
  </Grid>
</UserControl>



相关教程