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; using System.Data; namespace SiaSun.LMS.WPFClient.UC { /// /// ucTreeView.xaml 的交互逻辑 /// public partial class ucTreeView : UserControl { public delegate void U_ItemSelectedChangedHandler(TreeViewItem itemSelected); public event U_ItemSelectedChangedHandler U_ItemSelectedChanged; public delegate void U_ItemCheckedChangedHandler(string Header); public event U_ItemCheckedChangedHandler U_ItemCheckedChanged; #region ------自定义属性 bool boolAllowChecked = false; string _tableName = string.Empty; string _ItemHeaderColumnName = string.Empty; string _ItemTagColumnName = string.Empty; string _ItemParentColumn = string.Empty; string _ItemOrderColumn = string.Empty; string _QueryFilter = string.Empty; /// /// 控件描述 /// public string U_Header { set { this.grpBoxHeader.Header = string.Format(this.grpBoxHeader.Tag.ToString(),value); } } /// /// 是否允许显示复选框 /// public bool U_AllowCheck { get { return boolAllowChecked; } set { boolAllowChecked = value; } } /// /// 数据库表名 /// public string U_TableName { get { return _tableName; } } /// /// 树控件Header属性的关联字段 /// public string U_ItemHeaderColumnName { get { return _ItemHeaderColumnName; } } /// /// 树控件Tag属性的关联字段 /// public string U_ItemTagColumnName { get { return _ItemTagColumnName; } } /// /// 关联上级节点属性的字段 /// public string U_ItemParentColumn { get { return _ItemParentColumn; } } /// /// 树控件节点排序字段 /// public string U_ItemOrderColumn { get { return _ItemOrderColumn; } } /// /// 树控件筛选条件 /// public string U_QueryFilter { get { return _QueryFilter; } } /// /// 选中节点 /// public TreeViewItem U_SelectedItem { get { return tvwList.SelectedItem as TreeViewItem; } } #endregion public ucTreeView() { InitializeComponent(); } #region ------查询节点 /// /// 查询项目 /// private void btnQuery_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(txtQuickQuery.Text)) return; //获得节点列表 IEnumerable list = this.tvwList.Items.Cast(); TreeViewItem itemFinded = FindItem(null,list, this.txtQuickQuery.Text); if (itemFinded != null) { itemFinded.IsSelected = true; //展开节点 if (itemFinded.Parent != null && (itemFinded.Parent as TreeViewItem) != null) { (itemFinded.Parent as TreeViewItem).IsExpanded = true; } } } /// /// 回车键,查询节点 /// private void txtQuickQuery_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { btnQuery_Click(null, null); } } /// /// 根据查询条件获得节点 /// private TreeViewItem FindItem(TreeViewItem itemParent,IEnumerable list, string ItemHeader) { TreeViewItem itemFinded = null; if (itemParent == null) { if (list.Count(r => r.Header.ToString().Contains(ItemHeader)) > 0) { itemFinded = list.First(r => r.Header.ToString().Contains(ItemHeader)); return itemFinded; } } foreach (TreeViewItem item in list) { list = item.Items.Cast(); if (list.Count(r => r.Header.ToString().Contains(ItemHeader)) > 0) { itemFinded = list.First(r => r.Header.ToString().Contains(ItemHeader)); return itemFinded; } else { list = item.Items.Cast(); itemFinded = FindItem(item, list,ItemHeader); } } return itemFinded; } #endregion /// /// 加载树控件列表 /// public void U_LoadTreeViewItems(TreeViewItem ParentItem,string TableName, string ItemHeaderColumn, string ItemTagColumn, string QueryFilter, string OrderColumn) { this._tableName = TableName; this._ItemHeaderColumnName = ItemHeaderColumn; this._ItemTagColumnName = ItemTagColumn; this._QueryFilter = QueryFilter; this._ItemOrderColumn = OrderColumn; //查询语句 string strSql = string.Format("SELECT {0}, {1} FROM {2} WHERE {3}{4}", ItemHeaderColumn, ItemTagColumn, TableName, string.IsNullOrEmpty(_QueryFilter) ? "1=1" : QueryFilter, string.IsNullOrEmpty(OrderColumn) ? string.Empty : string.Format(" ORDER BY {0}", OrderColumn)); try { MainWindow.mainWin.Cursor = Cursors.Wait; //清空节点 if (ParentItem == null) this.tvwList.Items.Clear(); else ParentItem.Items.Clear(); using (DataTable tableSource = MainApp._I_BaseService.GetList(strSql)) { foreach (DataRow rowSource in tableSource.Rows) { TreeViewItem item = new TreeViewItem(); item.Style = (Style)this.Resources["styleTreeViewItem"]; item.Header = rowSource[ItemHeaderColumn].ToString(); item.Tag = rowSource[ItemTagColumn]; if (ParentItem == null) this.tvwList.Items.Add(item); else ParentItem.Items.Add(item); } ////默认选中第一个节点 //if (this.tvwList.HasItems) //{ // (this.tvwList.Items[0] as TreeViewItem).IsSelected = true; //} } this.txtQuickQuery.Focus(); } catch (Exception ex) { MainApp._MessageDialog.ShowException(ex); } finally { MainWindow.mainWin.Cursor = Cursors.Arrow; } } /// /// 加载树控件列表 /// public void U_LoadTreeViewItems(string RootNodeHeader,object RootNodeTag,string TableName,string ItemHeaderColumn,string ItemTagColumn,string ItemParentColumn,string ParentItemTagValue,string QueryFilter,string OrderColumn) { this._tableName = TableName; this._ItemHeaderColumnName = ItemHeaderColumn; this._ItemTagColumnName = ItemTagColumn; this._ItemParentColumn = ItemParentColumn; this._QueryFilter = QueryFilter; this._ItemOrderColumn = OrderColumn; //查询语句 string strSql = string.Format("SELECT {0},{1},{2}{5} FROM {3} WHERE {4}", ItemHeaderColumn, ItemTagColumn, ItemParentColumn, TableName, string.IsNullOrEmpty(_QueryFilter) ? "1=1" : QueryFilter, string.IsNullOrEmpty(OrderColumn)?string.Empty:string.Format(",{0}",OrderColumn)); try { MainWindow.mainWin.Cursor = Cursors.Wait; //清空节点 this.tvwList.Items.Clear(); //添加根级节点 TreeViewItem rootItem = this.U_AddTreeViewItem(null, RootNodeHeader, RootNodeTag); using (DataTable tableSource = MainApp._I_BaseService.GetList(strSql)) { this.AddTreeViewItem(tableSource, rootItem, ParentItemTagValue); ////默认选中第一个节点 //if (this.tvwList.HasItems) //{ // (this.tvwList.Items[0] as TreeViewItem).IsSelected = true; //} } //展开节点 rootItem.IsExpanded = true; this.txtQuickQuery.Focus(); } catch (Exception ex) { MainApp._MessageDialog.ShowException(ex); } finally { MainWindow.mainWin.Cursor = Cursors.Arrow; } } /// /// 添加树节点 /// private void AddTreeViewItem(DataTable tabeSource, TreeViewItem ParentItem, string ParentItemTagValue) { //查询视图 DataView dataView = new DataView(tabeSource); dataView.RowFilter = string.Format("{0}='{1}'", this._ItemParentColumn, ParentItemTagValue); dataView.Sort = this._ItemOrderColumn; //添加节点 TreeViewItem itemNew = null; foreach (DataRowView view in dataView) { itemNew = new TreeViewItem(); itemNew.Header = view[this._ItemHeaderColumnName].ToString(); itemNew.Tag = view[this._ItemTagColumnName]; if (ParentItem == null) this.tvwList.Items.Add(itemNew); else ParentItem.Items.Add(itemNew); //设置样式和模板 itemNew.Style = (Style)this.Resources["styleTreeViewItem"]; if (this.boolAllowChecked) { itemNew.Template = (ControlTemplate)this.Resources["templateCheckBoxTreeViewItem"]; } //添加子节点 AddTreeViewItem(tabeSource, itemNew,itemNew.Tag.ToString()); } } /// /// 添加节点 /// public TreeViewItem U_AddTreeViewItem(TreeViewItem ParentItem,string Header,object Tag) { TreeViewItem itemNew = null; try { itemNew = new TreeViewItem(); itemNew.Header = Header; itemNew.Tag = Tag; if (ParentItem == null) this.tvwList.Items.Add(itemNew); else ParentItem.Items.Add(itemNew); //设置样式和模板 itemNew.Style = (Style)this.Resources["styleTreeViewItem"]; if (this.boolAllowChecked) { itemNew.Template = (ControlTemplate)this.Resources["templateCheckBoxTreeViewItem"]; } } catch (Exception ex) { MainApp._MessageDialog.ShowException(ex); } return itemNew; } /// /// 节点更改 /// private void tvwList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e) { if (!this.boolAllowChecked) { if (this.U_ItemSelectedChanged != null) { TreeViewItem item = this.tvwList.SelectedItem as TreeViewItem; this.U_ItemSelectedChanged(item); } } } //点击复选框,确认选择 private void chkChecked_Click(object sender, RoutedEventArgs e) { //判断是否允许选中复选框 if (this.boolAllowChecked) { CheckBox chkBox = e.OriginalSource as CheckBox; if (chkBox != null) { StackPanel paneItem = chkBox.Parent as StackPanel; if (paneItem != null) { TextBlock txtHeader = paneItem.Children[1] as TextBlock; if (txtHeader != null) { if (this.U_ItemCheckedChanged != null) { this.U_ItemCheckedChanged(txtHeader.Text); } } } } } } /// /// 获得选中的节点查询条件 /// public string U_GetItemsQueryWhere() { string strWhere = null; //判断是否复选框 if (this.boolAllowChecked) { foreach (TreeViewItem item in tvwList.Items) { StackPanel panelItem = item.TemplatedParent as StackPanel; if (panelItem != null) { CheckBox chkBox = panelItem.Children.Cast().First(r => r is CheckBox) as CheckBox; if (chkBox.IsChecked == true) { strWhere = (string.IsNullOrEmpty(strWhere) ? string.Format("{0}='{1}'", _ItemTagColumnName, chkBox.Tag.ToString()) : string.Format(" OR {0}='{1}'", _ItemTagColumnName, chkBox.Tag.ToString())); } } } } else { //选择当前的选定的项目 if (this.tvwList.SelectedItem != null) { strWhere = string.Format("{0}='{1}'", _ItemTagColumnName,(this.tvwList.SelectedItem as TreeViewItem).Tag.ToString()); } } return strWhere; } /// /// 清空数据 /// public void U_Clear() { this.tvwList.Items.Clear(); } } }