資源簡介
WPF自定義控件,動態添加、刪除行,支持編輯,對外提供DataTable數據

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Data;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
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?UnityApp.Unity
{
????///?
????///?TableControl.xaml?的交互邏輯
????///?
????public?partial?class?TableControl?:?UserControl
????{
????????private?DataTable?_dt?=?new?DataTable();
????????public?TableControl()
????????{
????????????InitializeComponent();
????????????_dt.Columns.Add(new?DataColumn(“ParamKey“?typeof(string)));
????????????_dt.Columns.Add(new?DataColumn(“ParamValue“?typeof(string)));
????????????this.dgData.ItemsSource?=?null;
????????????this.dgData.ItemsSource?=?_dt.DefaultView;
????????}
????????#region?自定義依賴項屬性
????????///?
????????///?數據源
????????///?
????????public?DataTable?DataSource
????????{
????????????get?{?return?((DataView)this.dgData.ItemsSource).Table;?}
????????????set?{?SetValue(DataSourceProperty?value);?}
????????}
????????public?static?readonly?DependencyProperty?DataSourceProperty?=
????????????DependencyProperty.Register(“DataSource“?typeof(DataTable)?typeof(TableControl)?new?Propertymetadata(new?DataTable()?DataSourceChanged));
????????private?static?void?DataSourceChanged(Dependencyobject?d?DependencyPropertyChangedEventArgs?e)
????????{
????????????TableControl?control?=?d?as?TableControl;
????????????if?(e.NewValue?!=?e.OldValue)
????????????{
????????????????DataTable?dt?=?e.NewValue?as?DataTable;
????????????????control._dt?=?dt;
????????????????control.dgData.ItemsSource?=?null;
????????????????control.dgData.ItemsSource?=?control._dt.DefaultView;
????????????}
????????}
????????///?
????????///?是否編輯狀態
????????///?
????????public?bool?IsEdit
????????{
????????????get?{?return?(bool)GetValue(IsEditProperty);?}
????????????set?{?SetValue(IsEditProperty?value);?}
????????}
????????//?Using?a?DependencyProperty?as?the?backing?store?for?IsEdit.??This?enables?animation?styling?binding?etc...
????????public?static?readonly?DependencyProperty?IsEditProperty?=
????????????DependencyProperty.Register(“IsEdit“?typeof(bool)?typeof(TableControl)?new?Propertymetadata(true?IsEditChanged));
????????private?static?void?IsEditChanged(Dependencyobject?d?DependencyPropertyChangedEventArgs?e)
????????{
????????????TableControl?control?=?d?as?TableControl;
????????????bool?isEdit?=?Convert.ToBoolean(e.NewValue);
????????????if?(!isEdit)
????????????{
????????????????int?len?=?control.dgData.Columns.Count;
????????????????control.dgData.Columns[len?-?1].Visibility?=?Visibility.Collapsed;
????????????????control.dgData.IsReadOnly?=?true;
????????????????control.btnA
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????6680??2017-06-10?17:02??TableControl.xaml
?????文件???????3965??2017-06-10?16:42??TableControl.xaml.cs
-----------?---------??----------?-----??----
????????????????10645????????????????????2
- 上一篇:CSS樣式 (仿招聘網站)
- 下一篇:WPF資源字典中的控件事件觸發
評論
共有 條評論