| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 | using System;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Input;using InABox.Core;using Syncfusion.Windows.Controls.Input;namespace InABox.DynamicGrid{    public enum DynamicSplitPanelView    {        Master,        Detail,        Combined    }    public class DynamicSplitPanelChangedArgs : EventArgs    {        public DynamicSplitPanelView View { get; set; }        public double MasterWidth { get; set; }        public double DetailHeight { get; set; }    }    public delegate void DynamicSplitPanelChanged(object sender, DynamicSplitPanelChangedArgs e);    public class DynamicSplitPanel : Control    {        public static readonly DependencyProperty ViewProperty = DependencyProperty.Register("View", typeof(DynamicSplitPanelView),            typeof(DynamicSplitPanel), new UIPropertyMetadata(DynamicSplitPanelView.Detail));        //public static readonly DependencyProperty HeaderVisibilityProperty = DependencyProperty.Register("HeaderVisibility", typeof(Visibility), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));        //public Visibility HeaderVisibility        //{        //    get { return (Visibility)GetValue(HeaderVisibilityProperty); }        //    set        //    {        //        SetValue(HeaderVisibilityProperty, value);        //        ConfigureScreen();        //    }        //}        public static readonly DependencyProperty MasterWidthProperty =            DependencyProperty.Register("MasterWidth", typeof(double), typeof(DynamicSplitPanel), new UIPropertyMetadata((double)300F));        public static readonly DependencyProperty MasterCaptionProperty =            DependencyProperty.Register("MasterCaption", typeof(string), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));        public static readonly DependencyProperty DetailCaptionProperty =            DependencyProperty.Register("DetailCaption", typeof(string), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));        public static readonly DependencyProperty HeaderProperty =            DependencyProperty.Register("Header", typeof(FrameworkElement), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));        public static readonly DependencyProperty MasterProperty =            DependencyProperty.Register("Master", typeof(FrameworkElement), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));        public static readonly DependencyProperty DetailHeaderProperty =            DependencyProperty.Register("DetailHeader", typeof(FrameworkElement), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));        public static readonly DependencyProperty DetailProperty =            DependencyProperty.Register("Detail", typeof(FrameworkElement), typeof(DynamicSplitPanel), new UIPropertyMetadata(null));        public static readonly DependencyProperty DetailHeightProperty =            DependencyProperty.Register("DetailHeight", typeof(double), typeof(DynamicSplitPanel), new UIPropertyMetadata((double)200F));        public static readonly DependencyProperty SecondaryDetailProperty = DependencyProperty.Register("SecondaryDetail", typeof(FrameworkElement),            typeof(DynamicSplitPanel), new UIPropertyMetadata(null));        private Button CombinedLeft;        private Button CombinedRight;        private Grid DetailGrid;        private Label DetailsHeader;        private Button DetailsOnly;        private SfGridSplitter DetailSplitter;        private Grid Grid;        private Label MasterHeader;        private Button MasterOnly;        private SfGridSplitter Splitter;        static DynamicSplitPanel()        {            DefaultStyleKeyProperty.OverrideMetadata(typeof(DynamicSplitPanel), new FrameworkPropertyMetadata(typeof(DynamicSplitPanel)));        }        public DynamicSplitPanelView View        {            get => (DynamicSplitPanelView)GetValue(ViewProperty);            set            {                SetValue(ViewProperty, value);                ConfigureScreen();            }        }        public double MasterWidth        {            get => (double)GetValue(MasterWidthProperty);            set            {                SetValue(MasterWidthProperty, value);                ConfigureScreen();            }        }        public string MasterCaption        {            get => (string)GetValue(MasterCaptionProperty);            set => SetValue(MasterCaptionProperty, value);            //MasterHeader.Content = value;        }        public string DetailCaption        {            get => (string)GetValue(DetailCaptionProperty);            set => SetValue(DetailCaptionProperty, value);            //DetailHeader.Content = value;        }        public FrameworkElement Header        {            get => (FrameworkElement)GetValue(HeaderProperty);            set => SetValue(HeaderProperty, value);        }        public FrameworkElement Master        {            get => (FrameworkElement)GetValue(MasterProperty);            set => SetValue(MasterProperty, value);        }        public FrameworkElement DetailHeader        {            get => (FrameworkElement)GetValue(DetailHeaderProperty);            set => SetValue(DetailHeaderProperty, value);        }        public FrameworkElement Detail        {            get => (FrameworkElement)GetValue(DetailProperty);            set => SetValue(DetailProperty, value);        }        public double DetailHeight        {            get => (double)GetValue(DetailHeightProperty);            set            {                SetValue(DetailHeightProperty, value);                ConfigureScreen();            }        }        public FrameworkElement SecondaryDetail        {            get => (FrameworkElement)GetValue(SecondaryDetailProperty);            set => SetValue(SecondaryDetailProperty, value);        }        public event DynamicSplitPanelChanged OnChanged;        private void RightClick(object sender, RoutedEventArgs e)        {            if (View == DynamicSplitPanelView.Detail)                View = DynamicSplitPanelView.Combined;            else if (View == DynamicSplitPanelView.Combined)                View = DynamicSplitPanelView.Master;            Changed();        }        private void LeftClick(object sender, RoutedEventArgs e)        {            if (View == DynamicSplitPanelView.Master)                View = DynamicSplitPanelView.Combined;            else if (View == DynamicSplitPanelView.Combined)                View = DynamicSplitPanelView.Detail;            Changed();        }        private void ConfigureScreen()        {            CheckParts();            try            {                if (MasterHeader != null)                    MasterHeader.Content = MasterCaption;                if (DetailsHeader != null)                    DetailsHeader.Content = DetailCaption;                if (CombinedLeft != null)                    CombinedLeft.Visibility = View == DynamicSplitPanelView.Combined ? Visibility.Visible : Visibility.Collapsed;                if (CombinedRight != null)                    CombinedRight.Visibility = View == DynamicSplitPanelView.Combined ? Visibility.Visible : Visibility.Collapsed;                if (Grid != null)                {                    Grid.ColumnDefinitions[0].Width = View == DynamicSplitPanelView.Detail && Master != null                        ? new GridLength(1.0F, GridUnitType.Auto)                        : new GridLength(0.0F, GridUnitType.Pixel);                    Grid.ColumnDefinitions[1].Width = Master == null ? new GridLength(0.0F, GridUnitType.Pixel) :                        View == DynamicSplitPanelView.Master ? new GridLength(1.0F, GridUnitType.Star) :                        View == DynamicSplitPanelView.Detail ? new GridLength(0.0F, GridUnitType.Pixel) :                        new GridLength(MasterWidth, GridUnitType.Pixel);                    Grid.ColumnDefinitions[2].Width = View == DynamicSplitPanelView.Combined && Master != null                        ? new GridLength(1.0F, GridUnitType.Auto)                        : new GridLength(0.0F, GridUnitType.Pixel);                    Grid.ColumnDefinitions[3].Width = View == DynamicSplitPanelView.Master                        ? new GridLength(0.0F, GridUnitType.Star)                        : new GridLength(1.0F, GridUnitType.Star);                    Grid.ColumnDefinitions[4].Width = View == DynamicSplitPanelView.Master                        ? new GridLength(1.0F, GridUnitType.Auto)                        : new GridLength(0.0F, GridUnitType.Pixel);                    Grid.RowDefinitions[0].Height =                        Header == null ? new GridLength(0.0F, GridUnitType.Pixel) : new GridLength(1.0F, GridUnitType.Auto);                }                if (DetailGrid != null)                {                    DetailGrid.SetValue(Grid.RowProperty, DetailHeader == null ? 0 : 1);                    DetailGrid.SetValue(Grid.RowSpanProperty, DetailHeader == null ? 2 : 1);                    DetailGrid.RowDefinitions[0].Height = SecondaryDetail == null                        ? new GridLength(1.0F, GridUnitType.Star)                        : new GridLength(DetailHeight, GridUnitType.Pixel);                    DetailGrid.RowDefinitions[1].Height =                        SecondaryDetail == null ? new GridLength(0.0F, GridUnitType.Pixel) : new GridLength(1.0F, GridUnitType.Auto);                    DetailGrid.RowDefinitions[2].Height =                        SecondaryDetail == null ? new GridLength(0.0F, GridUnitType.Pixel) : new GridLength(1.0F, GridUnitType.Star);                }            }            catch (Exception e)            {                Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));            }        }        private void Splitter_PreviewMouseUp(object sender, MouseButtonEventArgs e)        {            SetValue(MasterWidthProperty, Grid.ColumnDefinitions[1].ActualWidth);            SetValue(DetailHeightProperty, SecondaryDetail == null ? 0.0F : DetailGrid.RowDefinitions[0].ActualHeight);            Changed();        }        private void Changed()        {            OnChanged?.Invoke(this, new DynamicSplitPanelChangedArgs { View = View, MasterWidth = MasterWidth, DetailHeight = DetailHeight });        }        public override void OnApplyTemplate()        {            base.OnApplyTemplate();            CheckParts();            ConfigureScreen();        }        private void CheckParts()        {            try            {                if (Grid == null)                    Grid = GetTemplateChild("PART_Grid") as Grid;                if (DetailGrid == null)                    DetailGrid = GetTemplateChild("PART_DetailGrid") as Grid;                if (MasterHeader == null)                    MasterHeader = GetTemplateChild("PART_MasterHeader") as Label;                if (DetailsHeader == null)                    DetailsHeader = GetTemplateChild("PART_DetailHeader") as Label;                if (Splitter == null)                {                    Splitter = GetTemplateChild("PART_Splitter") as SfGridSplitter;                    if (Splitter != null)                    {                        Splitter.PreviewMouseUp += Splitter_PreviewMouseUp;                        Splitter.TargetUpdated += Splitter_TargetUpdated;                    }                }                if (DetailSplitter == null)                {                    DetailSplitter = GetTemplateChild("PART_DetailSplitter") as SfGridSplitter;                    if (DetailSplitter != null)                        DetailSplitter.PreviewMouseUp += Splitter_PreviewMouseUp;                }                if (DetailsOnly == null)                {                    DetailsOnly = GetTemplateChild("PART_DetailsOnly") as Button;                    if (DetailsOnly != null)                        DetailsOnly.Click += RightClick;                }                if (CombinedRight == null)                {                    CombinedRight = GetTemplateChild("PART_CombinedRight") as Button;                    if (CombinedRight != null)                        CombinedRight.Click += RightClick;                }                if (CombinedLeft == null)                {                    CombinedLeft = GetTemplateChild("PART_CombinedLeft") as Button;                    if (CombinedLeft != null)                        CombinedLeft.Click += LeftClick;                }                if (MasterOnly == null)                {                    MasterOnly = GetTemplateChild("PART_MasterOnly") as Button;                    if (MasterOnly != null)                        MasterOnly.Click += LeftClick;                }            }            catch (Exception e)            {                Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));            }        }        private void Splitter_TargetUpdated(object sender, DataTransferEventArgs e)        {            //        }    }}
 |