| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 | using System;using System.Collections.Generic;using System.Configuration;using System.Linq;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{    [Flags]    public enum DynamicSplitPanelView    {        Master = 1,        Detail = 2,        Combined = 4    }    public enum DynamicSplitPanelAnchor    {        Master,        Detail    }    public class DynamicSplitPanelSettings : EventArgs    {        public DynamicSplitPanelView View { get; set; }        public double AnchorWidth { get; set; }        public double DetailHeight { get; set; }    }    public delegate void DynamicSplitPanelChanged(object sender, DynamicSplitPanelSettings e);    public class DynamicSplitPanel : Control    {                public static readonly DependencyProperty AllowableViewsProperty = DependencyProperty.Register(            nameof(AllowableViews),             typeof(DynamicSplitPanelView),            typeof(DynamicSplitPanel),             new UIPropertyMetadata( DynamicSplitPanelView.Master | DynamicSplitPanelView.Combined | DynamicSplitPanelView.Detail ));                public static readonly DependencyProperty ViewProperty = DependencyProperty.Register("View", typeof(DynamicSplitPanelView),            typeof(DynamicSplitPanel), new UIPropertyMetadata(DynamicSplitPanelView.Detail));        public static readonly DependencyProperty AnchorProperty = DependencyProperty.Register(nameof(Anchor), typeof(DynamicSplitPanelAnchor),            typeof(DynamicSplitPanel), new UIPropertyMetadata(DynamicSplitPanelAnchor.Master));                public static readonly DependencyProperty AnchorWidthProperty =            DependencyProperty.Register(nameof(AnchorWidth), 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 AllowableViews        {            get => (DynamicSplitPanelView)GetValue(AllowableViewsProperty);            set            {                SetValue(AllowableViewsProperty, value);                ConfigureScreen();            }                    }                    private bool IsViewAllowed(params DynamicSplitPanelView[] views)        {            foreach (var view in views)            {                if ((AllowableViews & view) == view)                    return true;            }            return false;        }        public DynamicSplitPanelView View        {            get => (DynamicSplitPanelView)GetValue(ViewProperty);            set            {                SetValue(ViewProperty, value);                ConfigureScreen();            }        }        public DynamicSplitPanelAnchor Anchor        {            get => (DynamicSplitPanelAnchor)GetValue(AnchorProperty);            set             {                SetValue(AnchorProperty, value);                ConfigureScreen();            }        }                public double AnchorWidth        {            get => (double)GetValue(AnchorWidthProperty);            set            {                SetValue(AnchorWidthProperty, 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)                                                       && (IsViewAllowed(DynamicSplitPanelView.Combined,DynamicSplitPanelView.Master))                        ? 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 switch {                             DynamicSplitPanelView.Master => new GridLength(1.0F, GridUnitType.Star),                            DynamicSplitPanelView.Detail => new GridLength(0.0F, GridUnitType.Pixel),                            _ => Anchor == DynamicSplitPanelAnchor.Master                                 ? new GridLength(AnchorWidth, GridUnitType.Pixel)                                 : new GridLength(1.0F, GridUnitType.Star)                        };                                        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 switch                    {                        DynamicSplitPanelView.Master => new GridLength(0.0F, GridUnitType.Pixel),                        DynamicSplitPanelView.Detail => new GridLength(1.0F, GridUnitType.Star),                        _ => Anchor == DynamicSplitPanelAnchor.Detail                            ? new GridLength(AnchorWidth, GridUnitType.Pixel)                            : new GridLength(1.0F, GridUnitType.Star)                    };                                            Grid.ColumnDefinitions[4].Width = (View == DynamicSplitPanelView.Master)                                                       && (IsViewAllowed(DynamicSplitPanelView.Combined,DynamicSplitPanelView.Detail))                        ? 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);                    DetailSplitter.IsEnabled = SecondaryDetail != null;                }                if (CombinedRight != null)                {                    CombinedRight.Visibility = (View == DynamicSplitPanelView.Combined) && IsViewAllowed(DynamicSplitPanelView.Master)                        ? Visibility.Visible                        : Visibility.Collapsed;                }                                if (CombinedLeft != null)                {                    CombinedLeft.Visibility = (View == DynamicSplitPanelView.Combined) && IsViewAllowed(DynamicSplitPanelView.Detail)                        ? Visibility.Visible                        : Visibility.Collapsed;                }            }            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)        {            if (Anchor == DynamicSplitPanelAnchor.Master)                SetValue(AnchorWidthProperty, Grid.ColumnDefinitions[1].ActualWidth);            else                 SetValue(AnchorWidthProperty, Grid.ColumnDefinitions[3].ActualWidth);            SetValue(DetailHeightProperty, SecondaryDetail == null ? 0.0F : DetailGrid.RowDefinitions[0].ActualHeight);            Changed();        }        private void Changed()        {            OnChanged?.Invoke(this, new DynamicSplitPanelSettings { View = View, AnchorWidth = AnchorWidth, 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)        {            //        }    }}
 |