csharp
// TreeView 全选/反选/子级选中父级也选中
xml
<TreeView Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Name="menuTree" Width="240" ItemsSource="{Binding MenuList}" BorderThickness="0" Background="#fff" HorizontalAlignment="Center" VerticalAlignment="Center">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=MenuChild}">
<StackPanel>
<CheckBox Content="{Binding Path=menu.Header}" Margin="0,3" x:Name="ckName" IsChecked="{Binding Path=IsSelect,Mode=TwoWay}" Tag="{Binding Path=menu.HeaderCode}" Checked="CkName_Checked" Unchecked="CkName_Unchecked"></CheckBox>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
<Setter Property="IsEnabled" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
csharp
/// <summary>
/// 选中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CkName_Checked(object sender, RoutedEventArgs e)
{
CheckBox ck = sender as CheckBox;
var list = viewModel.MenuList.ToList();
for (int i = 0; i < list.Count(); i++)
{
var item = list[i];
if (ck.Tag.ToString() == item.menu.HeaderCode)
{
item.IsSelect = true;
item.MenuChild = setBol(item.MenuChild, true, ck.Tag.ToString(), true);
}
else
{
item.MenuChild = setBol(item.MenuChild, true, ck.Tag.ToString());
}
}
gg(ck.Tag.ToString(), list);
}
/// <summary>
/// 修改是否选中
/// </summary>
/// <param name="list"></param>
/// <param name="bol"></param>
/// <returns></returns>
public List<MenuParent> setBol(List<MenuParent> list, bool bol, string str, bool dbol = false)
{
var d = list.ToList();
for (int i = 0; i < d.Count(); i++)
{
if (str == d[i].menu.HeaderCode || dbol)
{
d[i].IsSelect = bol;
if (d[i].MenuChild != null && d[i].MenuChild.Count > 0)
{
d[i].MenuChild = setBol(d[i].MenuChild, bol, str, true);
}
}
}
return d;
}
/// <summary>
/// 不选中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CkName_Unchecked(object sender, RoutedEventArgs e)
{
CheckBox ck = sender as CheckBox;
var list = viewModel.MenuList.ToList();
for (int i = 0; i < list.Count(); i++)
{
var item = list[i];
if (ck.Tag.ToString() == item.menu.HeaderCode)
{
item.IsSelect = false;
item.MenuChild = setBol(item.MenuChild, false, ck.Tag.ToString(), true);
}
else
{
item.MenuChild = setBol(item.MenuChild, false, ck.Tag.ToString());
}
}
gg(ck.Tag.ToString(),list);
}
/// <summary>
///
/// </summary>
/// <param name="cc"></param>
/// <param name="list"></param>
private void gg(string cc, List<MenuParent> list)
{
List<MenuParent> xin = new List<MenuParent>();
var r = viewModel.bphList(list.ToList()).Select(x => new MenuParent(x.IsSelect, x.menu)).OrderByDescending(x => x.menu.Remark).ToList();
var one = r.FirstOrDefault(x=>x.menu.HeaderCode == cc);
if (string.IsNullOrEmpty(one.menu.HeaderParent))
{
viewModel.MenuList = new System.Collections.ObjectModel.ObservableCollection<MenuParent>(viewModel.BuildTree(r.OrderBy(x => x.menu.Remark).ToList()));
return;
}
xin123 = new List<MenuParent>();
gg2(one.menu.Header,r);
for (int i = 0; i < r.Count(); i++)
{
var d = r[i];
if (xin123.Any(x => x.menu.Header == d.menu.Header))
{
d.IsSelect = xin123.FirstOrDefault(x=>x.menu.Header==d.menu.Header).IsSelect;
}
}
viewModel.MenuList = new System.Collections.ObjectModel.ObservableCollection<MenuParent>(viewModel.BuildTree(r.OrderBy(x=>x.menu.Remark).ToList()));
}
List<MenuParent> xin123 = new List<MenuParent>();
private void gg2(string cc, List<MenuParent> list,string s="")
{
var r = list.ToList();
var one = r.FirstOrDefault(x => x.menu.Header == cc);
if(!string.IsNullOrEmpty(s) && one.menu.Header != s)
{
if (r.Any(x => x.menu.HeaderParent == one.menu.Header && x.IsSelect))
{
one.IsSelect = true;
}
else
{
one.IsSelect = false;
}
xin123.Add(one);
for (int i = 0; i < r.Count(); i++)
{
var d = r[i];
if (xin123.Any(x => x.menu.Header == d.menu.Header))
{
d.IsSelect = xin123.FirstOrDefault(x => x.menu.Header == d.menu.Header).IsSelect;
}
}
}
if (!string.IsNullOrEmpty(one.menu.HeaderParent))
{
gg2(one.menu.HeaderParent,list, cc);
}
}
csharp
public class t_menu
{
/// <summary>
///
/// </summary>
public t_menu()
{
}
private System.Int32 _id;
/// <summary>
///
/// </summary>
public System.Int32 id { get { return this._id; } set { this._id = value; } }
private System.String _HeaderCode;
/// <summary>
///
/// </summary>
public System.String HeaderCode { get { return this._HeaderCode; } set { this._HeaderCode = value; } }
private System.String _Header;
/// <summary>
///
/// </summary>
public System.String Header { get { return this._Header; } set { this._Header = value; } }
private System.String _HeaderParent;
/// <summary>
///
/// </summary>
public System.String HeaderParent { get { return this._HeaderParent; } set { this._HeaderParent = value; } }
private System.String _Remark;
/// <summary>
///
/// </summary>
public System.String Remark { get { return this._Remark; } set { this._Remark = value; } }
private System.SByte? _ToRole;
/// <summary>
///
/// </summary>
public System.SByte? ToRole { get { return this._ToRole; } set { this._ToRole = value; } }
private System.SByte? _status;
/// <summary>
///
/// </summary>
public System.SByte? status { get { return this._status; } set { this._status = value; } }
}
public class MenuParent
{
public t_menu menu { get; set; }
/// <summary>
/// 是否选中
/// </summary>
public bool IsSelect { get; set; }
/// <summary>
/// 子菜单列表
/// </summary>
public List<MenuParent> MenuChild { get; set; }
public MenuParent(t_menu menu)
{
this.menu = menu;
this.IsSelect = false;
this.MenuChild = new List<MenuParent>();
}
public MenuParent(bool IsSelect,t_menu menu)
{
this.menu = menu;
this.IsSelect = IsSelect;
this.MenuChild = new List<MenuParent>();
}
}
csharp
// viewModel
// 初始化时
List<MenuParent> menuParents = new List<MenuParent>();
MenuList = new ObservableCollection<MenuParent>();
MenuList = new ObservableCollection<MenuParent>(BuildTree(menuParents));
private ObservableCollection<MenuParent> _menuList = new ObservableCollection<MenuParent>();
/// <summary>
/// 菜单列表
/// </summary>
public ObservableCollection<MenuParent> MenuList
{
get
{
return _menuList;
}
set
{
_menuList = value;
OnPropertyChanged(() => MenuList);
}
}
/// <summary>
/// 转为tree结构
/// </summary>
/// <param name="flatList"></param>
/// <returns></returns>
public List<MenuParent> BuildTree(List<MenuParent> menuItems)
{
// 创建一个字典来存储所有菜单项,以便快速查找
var menuDict = new Dictionary<string, MenuParent>();
// 遍历菜单项,将它们添加到字典中
foreach (var item in menuItems)
{
menuDict[item.menu.Header] = item;
}
// 创建一个列表来存储根菜单项(HeaderParent 为空或未设置的)
var rootMenuItems = new List<MenuParent>();
// 再次遍历菜单项,构建树形结构
foreach (var item in menuItems)
{
// 检查当前项是否有 HeaderParent
if (string.IsNullOrEmpty(item.menu.HeaderParent) || !menuDict.ContainsKey(item.menu.HeaderParent))
{
// 如果没有 HeaderParent 或找不到父节点,则它是根节点
rootMenuItems.Add(item);
}
else
{
// 找到父节点,并将当前项添加到其父节点的 Children 列表中
MenuParent parent = menuDict[item.menu.HeaderParent];
parent.MenuChild.Add(item);
}
}
// 返回根菜单项列表
return rootMenuItems;
}
/// <summary>
/// 获取list
/// </summary>
/// <returns></returns>
public List<MenuParent> bphList(List<MenuParent> menuParents)
{
List<MenuParent> menuParents1 = new List<MenuParent>();
if(menuParents != null && menuParents.Count>0)
{
foreach (var item in menuParents)
{
var temp = item;
if(temp != null && temp.MenuChild!=null && temp.MenuChild.Count>0)
{
var d = bphList(temp.MenuChild);
menuParents1.AddRange(d);
}
menuParents1.Add(item);
}
}
return menuParents1;
}