using KGIS.Framework.Views; using Kingo.PluginServiceInterface; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace Kingo.PlatformPlugin.Services { public class DockViewService : IDockViewService { private IDockPanel2 _DockPanel { get; set; } private List _Elements = new List(); public List Elements => _Elements; public EnumOrientation Orientation { get; set; } public bool IsShowSaveButton { get; set; } public Action Callback { get; set; } public void AddElement(IElementInfo obj) { if (_DockPanel == null) throw new Exception("视图未初始化,调用AddElements函数前,请先调用InitView函数进行初始化视图"); try { _Elements.Add(obj); _Elements = _Elements.OrderBy(e => e.ShowIndex).ToList(); Grid grid2 = (_DockPanel as System.Windows.Controls.UserControl).Content as Grid; Grid grid = null; Grid btnGrid = null; foreach (var item in grid2.Children) { if (item is Grid) { if ((item as Grid).Name == "ContentPanel") grid = item as Grid; if ((item as Grid).Name == "btnPanel") btnGrid = item as Grid; } } if (!IsShowSaveButton && btnGrid != null) { btnGrid.Visibility = Visibility.Collapsed; } if (grid == null) return; grid.Children.Clear(); grid.RowDefinitions.Clear(); grid.ColumnDefinitions.Clear(); int count = _Elements.Count; foreach (var item in _Elements) { if ((item as UserControl).Parent != null) { if (((item as UserControl).Parent as Grid) != null) ((item as UserControl).Parent as Grid).Children.Clear(); //(item as UserControl).Parent.SetValue(ContentPresenter.ContentProperty, null); } //grid.Children.Remove(item as UIElement); grid.Children.Add(item as UIElement); item.IsShow = true; if (Orientation == EnumOrientation.Vertical) { grid.RowDefinitions.Add(new RowDefinition()); if (item.DockHeight > 0) grid.RowDefinitions[grid.RowDefinitions.Count - 1].Height = new GridLength(item.DockHeight); if (item.ResetSize || _Elements.Count > 1) { grid.Children[grid.Children.Count - 1].SetValue(Grid.RowProperty, grid.RowDefinitions.Count - 1); GridSplitter split = new GridSplitter(); split.VerticalAlignment = VerticalAlignment.Bottom; split.HorizontalAlignment = HorizontalAlignment.Stretch; split.Height = 5; split.SetValue(Grid.RowProperty, grid.RowDefinitions.Count - 1); grid.Children.Add(split); } } else { grid.ColumnDefinitions.Add(new ColumnDefinition()); if (item.DockWidth > 0) grid.ColumnDefinitions[grid.ColumnDefinitions.Count - 1].Width = new GridLength(item.DockWidth); if (item.ResetSize || _Elements.Count > 1) { grid.Children[grid.Children.Count - 1].SetValue(Grid.ColumnProperty, grid.ColumnDefinitions.Count - 1); GridSplitter split = new GridSplitter(); split.VerticalAlignment = VerticalAlignment.Stretch; split.HorizontalAlignment = HorizontalAlignment.Right; split.Width = 5; grid.Children.Add(split); } } } grid.UpdateLayout(); } catch (Exception ex) { throw ex; } } public IDockPanel2 InitView() { if (_DockPanel == null) { View.DefaultDockView _view = new View.DefaultDockView(); _view.BtnCallBack += (s) => { if (s == "SaveEdit") { Save(); } else { Save(); Callback?.Invoke(s); } }; _DockPanel = _view; _DockPanel.CloseViewHandler += (s, a) => { Grid grid2 = (_DockPanel as System.Windows.Controls.UserControl).Content as Grid; Grid grid = null; Grid btnGrid = null; foreach (var item in grid2.Children) { if (item is Grid) { if ((item as Grid).Name == "ContentPanel") grid = item as Grid; if ((item as Grid).Name == "btnPanel") btnGrid = item as Grid; } } if (grid == null) return; grid.Children.Clear(); grid.RowDefinitions.Clear(); grid.ColumnDefinitions.Clear(); _DockPanel = null; }; } _Elements.Clear(); return _DockPanel; } public void Save() { try { foreach (var item in _Elements) { item.SaveEdit(); } } catch (Exception ex) { throw ex; } } public void RemoveElement(IElementInfo obj) { int idx = _Elements.IndexOf(obj); if (idx > -1) { _Elements.RemoveAt(idx); if (_DockPanel != null) { Grid grid = (_DockPanel as System.Windows.Controls.UserControl).Content as Grid; idx = grid.Children.IndexOf(obj as UIElement); if (idx > -1) { grid.Children.RemoveAt(idx); } } } } } }