?
1 2 3 4 5 6 7 8 9 10 |
|
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();
}
}
}
?
1 2 3 4 5 6 7 8 9 10 |
|
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