码迷,mamicode.com
首页 > 其他好文 > 详细

事件与委托例子

时间:2014-04-29 09:25:47      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   os   

1
2
3
4
5
6
7
8
9
10
<Window x:Class="DelegateAndEvent.MainWindow"
        Title="DelegateAndEvent" Height="410" Width="347">
    <DockPanel LastChildFill="True" Margin="5">
        <StackPanel Name="sp_list" DockPanel.Dock="Top" HorizontalAlignment="Center" VerticalAlignment="Top">
            <TextBlock Name="tb_sum" Width="271" Height="25"></TextBlock>
        </StackPanel>
    </DockPanel>
</Window>

  

mamicode.com,码迷
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DelegateAndEvent
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List _list;

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _list = new List();
            this.sp_list.Children.Insert(0, _list);


            _list.SumEvent += new List.SumEvenetHandler(_list_SumEvent);
           
        }

        void _list_SumEvent(List.SumEvenetArgs e)
        {
            this.tb_sum.Text = e.Count.ToString();
        }
    }
}
mamicode.com,码迷
1
2
3
4
5
6
7
8
9
10
<ListView x:Class="DelegateAndEvent.List"
        Loaded="ListView_Loaded">
    <ListView.Resources>
        <DataTemplate x:Key="dt_text">
            <TextBox Width="300" Height="25" Text="{Binding text}" Tag="{Binding tag}" TextChanged="TextBox_TextChanged" KeyDown="TextBox_KeyDown" GotFocus="TextBox_GotFocus"></TextBox>
        </DataTemplate>
    </ListView.Resources>
</ListView>

  

mamicode.com,码迷
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;
using System.Windows.Threading;
using System.Data;
using System.Data.Sql;

namespace DelegateAndEvent
{
    /// <summary>
    /// List.xaml 的交互逻辑
    /// </summary>
    public partial class List : ListView
    {
        public ListView objListView = new ListView();
        GridView objBillGridView = new GridView();
        int rows = 0;
        System.Data.DataTable dt = new System.Data.DataTable();
        TextBox tb;
        int tag;
        DispatcherTimer dst;

        /// <summary>
        /// 注册委托
        /// </summary>
        /// <param name="e"></param>
        public delegate void SumEvenetHandler(SumEvenetArgs e);

        /// <summary>
        /// 注册事件
        /// </summary>
        public event SumEvenetHandler SumEvent;

        public virtual void OnSumEvent()
        {
            if (SumEvent != null)
            {
                SumEvenetArgs e = new SumEvenetArgs(dt);
                SumEvent(e);
            }
        }

        public double SumCount;

        /// <summary>
        /// 
        /// </summary>
        public class SumEvenetArgs : EventArgs
        {
            private double count;

            public double Count
            {
                get { return count; }
                set { count = value; }
            }

            public SumEvenetArgs(DataTable dt)
            {
                this.count = (from p in dt.AsEnumerable()
                              select p).Sum(x => Convert.ToDouble(x.Field<string>("text")));
            }
        }

        public List()
        {
            InitializeComponent();
        }

        private void ListView_Loaded(object sender, RoutedEventArgs e)
        {
            objListView = sender as ListView;
            objBillGridView = new GridView();
            GridViewColumn gvc = new GridViewColumn();
            gvc.Header = "求和";
            gvc.CellTemplate = (DataTemplate)this.Resources["dt_text"];
            objBillGridView.Columns.Add(gvc);

            objListView.View = objBillGridView;
            
            dt.Columns.Add("text", typeof(string));
            dt.Columns.Add("tag", typeof(int));
            dt.Rows.Add();
            dt.Rows[rows]["text"] = "10";
            dt.Rows[rows]["tag"] = rows + 1;
            rows++;

            objListView.ItemsSource = dt.DefaultView;


            OnSumEvent();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tbx = sender as TextBox;
            if (tbx.Text == "")
            {
                return;
            }
            OnSumEvent();
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (tag == dt.Rows.Count)
                {
                    dt.Rows.Add();
                    dt.Rows[rows]["text"] = "0";
                    dt.Rows[rows]["tag"] = rows + 1;
                    rows++;
                }
                dst = new DispatcherTimer();
                dst.IsEnabled = true;
                dst.Start();
                dst.Interval = TimeSpan.FromSeconds(0.1);
                dst.Tick += new EventHandler(dst_Tick);
            }
        }

        void dst_Tick(object sender, EventArgs e)
        {
            dst.Stop();
            IEnumerable<TextBox> _tx = FindChildren<TextBox>(this);

            TextBox objcmbCtrls = _tx.ElementAt<TextBox>(tag) as TextBox;

            objcmbCtrls.Focus();
            dst = null;
            OnSumEvent();
        }

        private void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            tb = sender as TextBox;
            tag = int.Parse(tb.Tag.ToString());
            OnSumEvent();
        }

        #region 找控件
        public IEnumerable<T> FindChildren<T>(DependencyObject parent) where T : class
        {
            var count = VisualTreeHelper.GetChildrenCount(parent);
            if (count > 0)
            {
                for (var i = 0; i < count; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    var t = child as T;
                    if (t != null)
                        yield return t;

                    var children = FindChildren<T>(child);
                    foreach (var item in children)
                        yield return item;
                }
            }
        }
        #endregion
    }
}
mamicode.com,码迷

 

事件与委托例子,码迷,mamicode.com

事件与委托例子

标签:style   blog   http   java   color   os   

原文地址:http://www.cnblogs.com/demo8/p/3698477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!