首页 > 代码库 > 事件与委托例子
事件与委托例子
?
1 2 3 4 5 6 7 8 9 10 | <Window x:Class= "DelegateAndEvent.MainWindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" 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> |
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 | <ListView x:Class= "DelegateAndEvent.List" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" 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> |
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 } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。