Sfoglia il codice sorgente

更改菜单和角色方式

ltwork 1 anno fa
parent
commit
54661cb412

+ 77 - 0
BlankApp1/BlankApp1/Models/CheckBoxTreeViewModel.cs

@@ -0,0 +1,77 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PLCTool.Models
+{
+    public class CheckBoxTreeViewModel : Notify
+    {
+        private bool? _IsChecked = false;
+        private int _ID;
+        private string _Header = string.Empty;
+        private List<CheckBoxTreeViewModel> _Children = null;
+        private CheckBoxTreeViewModel _Parent = null;
+
+        public bool? IsChecked { get => _IsChecked; set { this.SetIsChecked(value, true, true); } }
+        public string Header { get => _Header; set => _Header = value; }
+        public List<CheckBoxTreeViewModel> Children { get => _Children; set { _Children = value; SetParentValue(); } }
+        public int ID { get => _ID; set => _ID = value; }
+        public CheckBoxTreeViewModel Parent { get => _Parent; private set => _Parent = value; }
+
+        /// <summary>
+        /// 设置节点IsChecked的值
+        /// </summary>
+        /// <param name="value"></param>
+        /// <param name="updateChildren"></param>
+        /// <param name="updateParent"></param>
+        private void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
+        {
+            if (value == _IsChecked)
+                return;
+            _IsChecked = value;
+            if (updateChildren && _IsChecked.HasValue && Children != null)
+            {
+                this.Children.ForEach(c => c.SetIsChecked(_IsChecked, true, false));
+            }
+            if (updateParent && Parent != null)
+            {
+                Parent.VerifyCheckState();
+            }
+            this.RaisePropertyChanged("IsChecked");
+        }
+        /// <summary>
+        /// 验证并设置父级节点的IsChecked的值
+        /// </summary>
+        private void VerifyCheckState()
+        {
+            bool? state = null;
+            for (int i = 0; i < this.Children.Count; ++i)
+            {
+                bool? current = this.Children[i].IsChecked;
+                if (i == 0)
+                {
+                    state = current;
+                }
+                else if (state != current)
+                {
+                    state = null;
+                    break;
+                }
+            }
+            this.SetIsChecked(state, false, true);
+        }
+        /// <summary>
+        /// 数据初始化时设置父节点的值
+        /// </summary>
+        private void SetParentValue()
+        {
+            if (this.Children != null)
+            {
+                this.Children.ForEach(ch => ch.Parent = this);
+            }
+        }
+    }
+
+}

+ 19 - 0
BlankApp1/BlankApp1/Models/Notify.cs

@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PLCTool.Models
+{
+    public class Notify : INotifyPropertyChanged
+    {
+        public event PropertyChangedEventHandler PropertyChanged;
+        protected virtual void RaisePropertyChanged(string propertyname)
+        {
+            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
+        }
+    }
+
+}

+ 155 - 42
BlankApp1/BlankApp1/ViewModels/SystemManageViewModel/Dialogs/RoleWithMenuViewModel.cs

@@ -15,6 +15,8 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows;
+using WPFDevelopers.Helpers;
+using static System.Windows.Forms.VisualStyles.VisualStyleElement;
 
 namespace PLCTool.ViewModels.SystemManageViewModel.Dialogs
 {
@@ -28,7 +30,7 @@ namespace PLCTool.ViewModels.SystemManageViewModel.Dialogs
         private readonly IRoleMenuService _iRoleMenuService;
         private string tyepName = String.Empty;
         private int roleId;
-
+        private List<Model.Entities.Menu> allMenus = new List<Model.Entities.Menu>();
         public RoleWithMenuViewModel(IEventAggregator aggregator, IRoleService iRoleService,IMenuService iMenuService, IRoleMenuService iRoleMenuService,   IMapper mapper, ILogger logger)
         {
 
@@ -40,43 +42,102 @@ namespace PLCTool.ViewModels.SystemManageViewModel.Dialogs
             _iRoleService = iRoleService;
             SureCommand = new DelegateCommand<string>(Sure);
          
-            SelectAllCommand = new DelegateCommand<object>(SelectAll);
-            UnSelectAllCommand = new DelegateCommand<object>(UnSelecttAll);
+ 
            
         }
 
         private void GetAllMenus()
         {
-            var menus = _iMenuService.QueryList();
-            foreach (var menu in menus)
+            allMenus = _iMenuService.QueryList();
+            //按照父菜单和子菜单来排序
+            var menuParents = allMenus.FindAll(x => x.ParentId == 0)?.OrderBy(y => y.Index);
+            
             {
-                //查找角色是否带有这个菜单
-                bool isSelect = false;
-
-                var findMenu= _iRoleMenuService.FindAllByRoleId(roleId).FirstOrDefault(x=>x.MenuId==menu.MenuId);
-                if(findMenu != null)
+                foreach (var menu in menuParents)
                 {
-                    isSelect = true;
+
+                    List<CheckBoxTreeViewModel> itemChilds = new List<CheckBoxTreeViewModel>();
+                    //查找下面的子菜单
+                    var menuPs = allMenus?.FindAll(x => x.ParentId == menu.MenuId)?.OrderBy(y => y.Index);
+                    if(menuPs.Count()==0) //父节点下没有子节点
+                    {
+                        bool isSelect = false;
+
+                        var findMenu = _iRoleMenuService.FindAllByRoleId(roleId).FirstOrDefault(x => x.MenuId == menu.MenuId);
+                        if (findMenu != null)
+                        {
+                            isSelect = true;
+                        }
+                        CheckBoxTreeViewModel itemEach = new CheckBoxTreeViewModel()
+                        {
+                            Header = menu.MenuHeader,
+                            Children = null,
+                            IsChecked=isSelect,
+                        };
+                        Items.Add(itemEach);
+                    }
+                    else //父节点下有子节点
+                    {
+                        foreach (var item in menuPs)
+                        { //查找角色是否带有这个菜单
+                            bool isSelect = false;
+
+                            var findMenu = _iRoleMenuService.FindAllByRoleId(roleId).FirstOrDefault(x => x.MenuId == item.MenuId);
+                            if (findMenu != null)
+                            {
+                                isSelect = true;
+                            }
+                            //赋值
+                            CheckBoxTreeViewModel child = new CheckBoxTreeViewModel
+                            {
+                                Header = item.MenuHeader,
+                                IsChecked = isSelect,
+                                Children = null,
+                            };
+                            itemChilds.Add(child);
+
+
+                        }
+                        //父节点
+                        CheckBoxTreeViewModel itemEach = new CheckBoxTreeViewModel()
+                        {
+                            Header = menu.MenuHeader,
+                            Children = itemChilds,
+                        };
+
+                        //判断父节点的选择状态
+                        var selectNum = itemChilds.FindAll(x => x.IsChecked == true);
+                        if ((selectNum != null) && (selectNum.Count == 0))
+                        {
+                            itemEach.IsChecked = false;
+                        }
+                        else
+                        {
+                            //全部选择
+                            if (selectNum.Count == itemChilds.Count)
+                            {
+                                itemEach.IsChecked = true;
+                            }
+                            else
+                            {
+                                //部分选中
+                                itemEach.IsChecked = null;
+                            }
+                        }
+
+
+                        Items.Add(itemEach);
+                    }
+           
+
                 }
-                RoleWithMenuList.Add(new SelectRoleMenuDto
-                {
-                    MenuId=menu.MenuId,
-                    MenuHeader = menu.MenuHeader,
-                    Describe = menu.ParentId == 0 ? "一级菜单" : "二级菜单",
-                    IsSelected = isSelect
-                });
+
             }
+           
+        
         }
 
-        private void UnSelecttAll(object obj)
-        {
-            RoleWithMenuList.ToList().FindAll(p => p.IsSelected = false);
-        }
-
-        private void SelectAll(object obj)
-        {
-            RoleWithMenuList.ToList().FindAll(p => p.IsSelected = true);
-        }
+    
 
         
 
@@ -85,12 +146,12 @@ namespace PLCTool.ViewModels.SystemManageViewModel.Dialogs
         {
             //查找是否有这个测试方案
 
-            var findSelect = RoleWithMenuList.FirstOrDefault(x => x.IsSelected == true);
-            if (findSelect == null)
-            {
-                MessageBoxResult boxResult = MessageBox.Show("请勾选测试项!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
-                return;
-            }
+            //var findSelect = RoleWithMenuList.FirstOrDefault(x => x.IsSelected == true);
+            //if (findSelect == null)
+            //{
+            //    MessageBoxResult boxResult = MessageBox.Show("请勾选测试项!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
+            //    return;
+            //}
             //查找的信息
             //删除这个角色以前的菜单
             var findMenus = _iRoleMenuService.FindAllByRoleId(roleId);
@@ -99,17 +160,64 @@ namespace PLCTool.ViewModels.SystemManageViewModel.Dialogs
                 _iRoleMenuService.Delete(item.Id);
             }
             //添加角色关联的菜单到数据库
-            var roleMeuns = RoleWithMenuList.ToList().FindAll(x => x.IsSelected).OrderBy(y => y.MenuId);
-            foreach (var item in roleMeuns)
+            foreach(var item in Items)
             {
-                _iRoleMenuService.Add(new RoleMenu
+                //表示是父菜单下还有子菜单
+                if(item.Children!=null&&(item.Children.Count!=0))
                 {
-                    RoleId = roleId,
-                    MenuId = item.MenuId
-                });
+                    //表示部分选择或全选
+                    if(item.IsChecked!=false)
+                    {
+                        //添加父菜单
+                        string menu = item.Header;
+
+                        _iRoleMenuService.Add(new RoleMenu
+                        {
+                            RoleId = roleId,
+                            MenuId = allMenus.FirstOrDefault(x => x.MenuHeader == menu)?.MenuId
+                        });
+                        //父菜单下子菜单
+                        foreach (var child in item.Children)
+                        {
+                            if (child.IsChecked== true)
+                            {
+                                string childMenu = child.Header;
+                                _iRoleMenuService.Add(new RoleMenu
+                                {
+                                    RoleId = roleId,
+                                    MenuId = allMenus.FirstOrDefault(x => x.MenuHeader == childMenu)?.MenuId
+                                });
+                            }
+                         
+                        }
+                     
+                    }
+                  
+
+                }
+                //表示父菜单下没有子菜单了
+                else
+                {
+                    //表示选
+                    if (item.IsChecked == true)
+                    {
+                        string menu = item.Header;
+
+                        _iRoleMenuService.Add(new RoleMenu
+                        {
+                            RoleId = roleId,
+                            MenuId = allMenus.FirstOrDefault(x => x.MenuHeader == menu)?.MenuId
+                        });
+                    }
+                }
             }
+
+           
             RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
         }
+       
+
+    
 
         public string Title { set; get; } = "授权角色菜单";
 
@@ -140,6 +248,8 @@ namespace PLCTool.ViewModels.SystemManageViewModel.Dialogs
                     roleId = item;
                     GetAllMenus();
                     RoleName = _iRoleService.Find(roleId).Name;
+
+                   
                 }
              
 
@@ -151,9 +261,6 @@ namespace PLCTool.ViewModels.SystemManageViewModel.Dialogs
         public DelegateCommand<string> SureCommand { set; get; }
 
 
-        public DelegateCommand<object> SelectAllCommand { set; get; }
-        public DelegateCommand<object> UnSelectAllCommand { set; get; }
-
         #endregion
         #region 数据绑定
         /// <summary>
@@ -174,7 +281,13 @@ namespace PLCTool.ViewModels.SystemManageViewModel.Dialogs
             get { return roleWithMenuList; }
             set { SetProperty(ref roleWithMenuList, value); }
         }
+        private ObservableCollection<CheckBoxTreeViewModel> _Items = new ObservableCollection<CheckBoxTreeViewModel>();
 
+        public ObservableCollection<CheckBoxTreeViewModel> Items
+        {
+            get { return _Items; }
+            set { _Items = value; RaisePropertyChanged(); }
+        }
 
         #endregion
 

+ 22 - 3
BlankApp1/BlankApp1/Views/SystemManageView/Dialogs/RoleWithMenuView.xaml

@@ -5,7 +5,7 @@
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              xmlns:local="clr-namespace:PLCTool.Views.SystemManageView.Dialogs"
              xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
-             Height="600" Width="800">
+             Height="600" Width="500">
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="60" />
@@ -18,7 +18,7 @@
             <Button  Content="确认授权" Width="80"  Margin="5,0"  Command="{Binding SureCommand}" Style="{StaticResource NormalButtonStyle}" />
         </DockPanel>
         <Grid Grid.Row="1">
-            <DataGrid  Grid.Row="2"  Style="{StaticResource MyDataGridSyle}"
+            <!--<DataGrid  Grid.Row="2"  Style="{StaticResource MyDataGridSyle}"
              ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"  RowHeaderStyle="{StaticResource RowHeaderStyle}" RowStyle="{StaticResource DataGridRowtyle}"  AlternationCount="2"
             ItemsSource="{Binding RoleWithMenuList}"  >
 
@@ -50,7 +50,26 @@
                  
                 </DataGrid.Columns>
 
-            </DataGrid>
+            </DataGrid>-->
+
+            <TreeView ItemsSource="{Binding Items}"  >
+                <TreeView.ItemContainerStyle>
+                    <Style TargetType="{x:Type TreeViewItem}">
+
+                        <Setter Property="IsExpanded" Value="True"></Setter>
+                    </Style>
+                </TreeView.ItemContainerStyle>
+
+                <TreeView.ItemTemplate>
+                    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
+                        <StackPanel Orientation="Horizontal">
+                            <CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></CheckBox>
+                            <TextBlock Text="{Binding Header}"></TextBlock>
+                        </StackPanel>
+                    </HierarchicalDataTemplate>
+                </TreeView.ItemTemplate>
+            </TreeView>
+
         </Grid>
     </Grid>
 </UserControl>