//***************************************************//
//文件名(FileName) : ZsmTreeView.xaml.cs//
//作者(Author) : zsm//
//创建时间(CreateAt): 2013-03-15 16:52:40//
//描述(Description) : 带CheckBox的TreeView控件的交互逻辑代码//
//***************************************************
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespaceCom.FMS.View.UserControls
{///
///ZsmTreeView.xaml 的交互逻辑///
public partial classZsmTreeView : UserControl
{#region 私有变量属性
///
///控件数据///
private IList_itemsSourceData;#endregion
///
///构造///
publicZsmTreeView()
{
InitializeComponent();
}///
///控件数据///
public IListItemsSourceData
{get { return_itemsSourceData; }set{
_itemsSourceData=value;
tvZsmTree.ItemsSource=_itemsSourceData;
}
}///
///设置对应Id的项为选中状态///
///
///
public int SetCheckedById(string id, IListtreeList)
{foreach (var tree intreeList)
{if(tree.Id.Equals(id))
{
tree.IsChecked= true;return 1;
}if (SetCheckedById(id, tree.Children) == 1)
{return 1;
}
}return 0;
}///
///设置对应Id的项为选中状态///
///
///
public int SetCheckedById(stringid)
{foreach (var tree inItemsSourceData)
{if(tree.Id.Equals(id))
{
tree.IsChecked= true;return 1;
}if (SetCheckedById(id, tree.Children) == 1)
{return 1;
}
}return 0;
}///
///获取选中项///
///
public IListCheckedItemsIgnoreRelation()
{returnGetCheckedItemsIgnoreRelation(_itemsSourceData);
}///
///私有方法,忽略层次关系的情况下,获取选中项///
///
///
private IList GetCheckedItemsIgnoreRelation(IListlist)
{
IList treeList = new List();foreach (var tree inlist)
{if(tree.IsChecked)
{
treeList.Add(tree);
}foreach (var child inGetCheckedItemsIgnoreRelation(tree.Children))
{
treeList.Add(child);
}
}returntreeList;
}///
///选中所有子项菜单事件///
///
///
private void menuSelectAllChild_Click(objectsender, RoutedEventArgs e)
{if (tvZsmTree.SelectedItem != null)
{
Model.TreeModel tree=(Model.TreeModel)tvZsmTree.SelectedItem;
tree.IsChecked= true;
tree.SetChildrenChecked(true);
}
}///
///全部展开菜单事件///
///
///
private void menuExpandAll_Click(objectsender, RoutedEventArgs e)
{foreach (Model.TreeModel tree intvZsmTree.ItemsSource)
{
tree.IsExpanded= true;
tree.SetChildrenExpanded(true);
}
}///
///全部折叠菜单事件///
///
///
private void menuUnExpandAll_Click(objectsender, RoutedEventArgs e)
{foreach (Model.TreeModel tree intvZsmTree.ItemsSource)
{
tree.IsExpanded= false;
tree.SetChildrenExpanded(false);
}
}///
///全部选中事件///
///
///
private void menuSelectAll_Click(objectsender, RoutedEventArgs e)
{foreach (Model.TreeModel tree intvZsmTree.ItemsSource)
{
tree.IsChecked= true;
tree.SetChildrenChecked(true);
}
}///
///全部取消选中///
///
///
private void menuUnSelectAll_Click(objectsender, RoutedEventArgs e)
{foreach (Model.TreeModel tree intvZsmTree.ItemsSource)
{
tree.IsChecked= false;
tree.SetChildrenChecked(false);
}
}///
///鼠标右键事件///
///
///
private void TreeViewItem_PreviewMouseRightButtonDown(objectsender, MouseButtonEventArgs e)
{
TreeViewItem item= VisualUpwardSearch(e.OriginalSource as DependencyObject) asTreeViewItem;if (item != null)
{
item.Focus();
e.Handled= true;
}
}static DependencyObject VisualUpwardSearch(DependencyObject source)
{while (source != null && source.GetType() != typeof(T))
source=VisualTreeHelper.GetParent(source);returnsource;
}
}
}