wpf 带复选框的treeview_WPF 带CheckBox、图标的TreeView

//***************************************************//

//文件名(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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值