void EditorMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragDropEffects  allowedEffects = DragDropEffects.Move;
            GridLengthEditor editor         = sender as GridLengthEditor;

            DragDrop.DoDragDrop(editor, editor, allowedEffects);
        }
        void EditorDragOver(object sender, DragEventArgs e)
        {
            try {
                GridLengthEditor target = sender as GridLengthEditor;
                GridLengthEditor source = e.Data.GetData(typeof(GridLengthEditor)) as GridLengthEditor;
                e.Handled = true;

                if (marker != null)
                {
                    AdornerLayer.GetAdornerLayer(marker.AdornedElement).Remove(marker);
                    marker = null;
                }

                if (target != null && source != null && source.Orientation == target.Orientation &&
                    (target != source && (target.Cell < source.Cell || target.Cell > source.Cell + 1)))
                {
                    marker    = DragDropMarkerAdorner.CreateAdornerCellMove(target);
                    e.Effects = DragDropEffects.Move;
                    return;
                }

                e.Effects = DragDropEffects.None;
            } catch (Exception ex) {
                Core.LoggingService.Error(ex);
            }
        }
        void ButtonPanelDragOver(object sender, DragEventArgs e)
        {
            try {
                StackPanel       target = sender as StackPanel;
                GridLengthEditor source = e.Data.GetData(typeof(GridLengthEditor)) as GridLengthEditor;
                e.Handled = true;

                if (marker != null)
                {
                    AdornerLayer.GetAdornerLayer(marker.AdornedElement).Remove(marker);
                    marker = null;
                }

                if (target != null && source != null && source.Orientation == Orientation.Vertical && source.Cell + 1 < rowDefitions.Elements().Count())
                {
                    marker    = DragDropMarkerAdorner.CreateAdornerCellMove(target);
                    e.Effects = DragDropEffects.Move;
                    return;
                }

                e.Effects = DragDropEffects.None;
            } catch (Exception ex) {
                Core.LoggingService.Error(ex);
            }
        }
        void DropPanelDrop(object sender, DragEventArgs e)
        {
            try {
                GridLengthEditor source = e.Data.GetData(typeof(GridLengthEditor)) as GridLengthEditor;

                if (source != null)
                {
                    MoveRight(source.Cell, Math.Abs(source.Cell - colDefitions.Elements().Count()) - 1);
                }
            } catch (Exception ex) {
                Core.LoggingService.Error(ex);
            }
        }
Esempio n. 5
0
        public static DragDropMarkerAdorner CreateAdornerCellMove(FrameworkElement element)
        {
            DragDropMarkerAdorner adorner = new DragDropMarkerAdorner(element);

            if (element is GridLengthEditor)
            {
                GridLengthEditor editor = element as GridLengthEditor;
                if (editor.Orientation == Orientation.Horizontal)
                {
                    adorner.start = new Point(5, 5);
                    adorner.end   = new Point(5, editor.ActualWidth + 50);
                }
                else
                {
                    adorner.start = new Point(5, 5);
                    adorner.end   = new Point(editor.ActualHeight + 250, 5);
                }
            }
            else if (element is StackPanel)
            {
                StackPanel panel     = element as StackPanel;
                Dock       dockState = (Dock)panel.GetValue(DockPanel.DockProperty);
                if (dockState == Dock.Right)
                {
                    adorner.start = new Point(5, 5);
                    adorner.end   = new Point(5, panel.ActualHeight);
                }
                else if (dockState == Dock.Bottom)
                {
                    adorner.start = new Point(5, 5);
                    adorner.end   = new Point(panel.ActualWidth - 10, 5);
                }
            }

            AdornerLayer.GetAdornerLayer(element).Add(adorner);

            return(adorner);
        }
        void EditorDrop(object sender, DragEventArgs e)
        {
            try {
                GridLengthEditor source = e.Data.GetData(typeof(GridLengthEditor)) as GridLengthEditor;
                GridLengthEditor target = sender as GridLengthEditor;

                if (source != null && target != null)
                {
                    if (source.Orientation == Orientation.Horizontal)
                    {
                        if (source.Cell > target.Cell)
                        {
                            MoveLeft(source.Cell, Math.Abs(source.Cell - target.Cell));
                        }
                        else
                        {
                            MoveRight(source.Cell, Math.Abs(source.Cell - target.Cell) - 1);
                        }
                    }

                    if (source.Orientation == Orientation.Vertical)
                    {
                        if (source.Cell > target.Cell)
                        {
                            MoveUp(source.Cell, Math.Abs(source.Cell - target.Cell));
                        }
                        else
                        {
                            MoveDown(source.Cell, Math.Abs(source.Cell - target.Cell) - 1);
                        }
                    }
                }
            } catch (Exception ex) {
                Core.LoggingService.Error(ex);
            }
        }
		void RebuildGrid()
		{
			if (this.marker != null) {
				AdornerLayer.GetAdornerLayer(this.buttonPanel).Remove(this.marker);
				AdornerLayer.GetAdornerLayer(this.dropPanel).Remove(this.marker);
				this.marker = null;
			}
			
			this.gridDisplay.Children.Clear();
			this.gridDisplay.RowDefinitions.Clear();
			this.gridDisplay.ColumnDefinitions.Clear();
			
			
			this.columnWidthGrid.ColumnDefinitions.Clear();
			this.columnWidthGrid.Children.Clear();
			
			this.rowHeightGrid.RowDefinitions.Clear();
			this.rowHeightGrid.Children.Clear();
			
			int rows = rowDefitions.Elements().Count();
			int cols = colDefitions.Elements().Count();
			
			if (rows == 0) {
				rowDefitions.Add(new XElement(rowDefName).AddAttribute("Height", "*"));
				rows = 1;
			}
			if (cols == 0) {
				colDefitions.Add(new XElement(colDefName).AddAttribute("Width", "*"));
				cols = 1;
			}
			
			for (int i = 0; i < cols; i++) {
				this.gridDisplay.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
				this.columnWidthGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
				GridLengthEditor editor = new GridLengthEditor(Orientation.Horizontal, i, (colDefitions.Elements().ElementAt(i).Attribute("Width") ?? new XAttribute("Width", "")).Value);
				
				editor.SelectedValueChanged += new EventHandler<GridLengthSelectionChangedEventArgs>(EditorSelectedValueChanged);
				editor.Deleted += new EventHandler<GridLengthSelectionChangedEventArgs>(EditorDeleted);
				editor.MouseLeftButtonDown += new MouseButtonEventHandler(EditorMouseLeftButtonDown);
				editor.Drop += new DragEventHandler(EditorDrop);
				editor.DragOver += new DragEventHandler(EditorDragOver);
				
				editor.AllowDrop = true;
				
				this.columnWidthGrid.Children.Add(editor);
				
				Button leftAddButton = new Button() {
					Content = "+",
					HorizontalAlignment = HorizontalAlignment.Left,
					Margin = new Thickness(-10, 10, 5,10),
					Padding = new Thickness(3),
					Tag = i
				};
				
				leftAddButton.Click += BtnAddColumnClick;
				
				leftAddButton.SetValue(Grid.ColumnProperty, i);
				this.columnWidthGrid.Children.Add(leftAddButton);
				
				if (cols == i + 1) {
					Button rightAddButton = new Button() {
						Content = "+",
						HorizontalAlignment = HorizontalAlignment.Right,
						Margin = new Thickness(5, 10, 0, 10),
						Padding = new Thickness(3)
					};
					
					rightAddButton.Click += BtnAddColumnClick;
					
					rightAddButton.SetValue(Grid.ColumnProperty, i);
					this.columnWidthGrid.Children.Add(rightAddButton);
				}
			}
			
			for (int i = 0; i < rows; i++) {
				this.gridDisplay.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
				
				this.rowHeightGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
				GridLengthEditor editor = new GridLengthEditor(Orientation.Vertical, i, (rowDefitions.Elements().ElementAt(i).Attribute("Height") ?? new XAttribute("Height", "")).Value);
				
				editor.SelectedValueChanged += new EventHandler<GridLengthSelectionChangedEventArgs>(EditorSelectedValueChanged);
				editor.Deleted += new EventHandler<GridLengthSelectionChangedEventArgs>(EditorDeleted);
				editor.MouseLeftButtonDown += new MouseButtonEventHandler(EditorMouseLeftButtonDown);
				editor.Drop += new DragEventHandler(EditorDrop);
				editor.DragOver += new DragEventHandler(EditorDragOver);
				
				editor.AllowDrop = true;
				
				this.rowHeightGrid.Children.Add(editor);
				
				Button topAddButton = new Button() {
					Content = "+",
					VerticalAlignment = VerticalAlignment.Top,
					Margin = new Thickness(10, -10, 10, 5),
					Padding = new Thickness(3),
					Tag = i
				};
				
				topAddButton.Click += BtnAddRowClick;
				
				topAddButton.SetValue(Grid.RowProperty, i);
				this.rowHeightGrid.Children.Add(topAddButton);
				
				if (rows == i + 1) {
					Button bottomAddButton = new Button() {
						Content = "+",
						VerticalAlignment = VerticalAlignment.Bottom,
						Margin = new Thickness(10, 5, 10, 0),
						Padding = new Thickness(3)
					};
					
					bottomAddButton.Click += BtnAddRowClick;
					
					bottomAddButton.SetValue(Grid.RowProperty, i);
					this.rowHeightGrid.Children.Add(bottomAddButton);
				}
				
				for (int j = 0; j < cols; j++) {
					StackPanel displayRect = new StackPanel() {
						Margin = new Thickness(5),
						Background = Brushes.LightGray,
						Orientation = Orientation.Vertical
					};
					
					displayRect.AllowDrop = true;
					
					displayRect.Drop += new DragEventHandler(DisplayRectDrop);
					displayRect.DragOver += new DragEventHandler(DisplayRectDragOver);
					
					displayRect.Children.AddRange(BuildItemsForCell(i, j));
					
					displayRect.SetValue(Grid.RowProperty, i);
					displayRect.SetValue(Grid.ColumnProperty, j);
					
					displayRect.ContextMenuOpening += new ContextMenuEventHandler(DisplayRectContextMenuOpening);
					
					this.gridDisplay.Children.Add(displayRect);
				}
			}
			
			this.InvalidateVisual();
		}
        void RebuildGrid()
        {
            if (this.marker != null)
            {
                AdornerLayer.GetAdornerLayer(this.buttonPanel).Remove(this.marker);
                AdornerLayer.GetAdornerLayer(this.dropPanel).Remove(this.marker);
                this.marker = null;
            }

            this.gridDisplay.Children.Clear();
            this.gridDisplay.RowDefinitions.Clear();
            this.gridDisplay.ColumnDefinitions.Clear();


            this.columnWidthGrid.ColumnDefinitions.Clear();
            this.columnWidthGrid.Children.Clear();

            this.rowHeightGrid.RowDefinitions.Clear();
            this.rowHeightGrid.Children.Clear();

            int rows = rowDefitions.Elements().Count();
            int cols = colDefitions.Elements().Count();

            if (rows == 0)
            {
                rowDefitions.Add(new XElement(rowDefName).AddAttribute("Height", "*"));
                rows = 1;
            }
            if (cols == 0)
            {
                colDefitions.Add(new XElement(colDefName).AddAttribute("Width", "*"));
                cols = 1;
            }

            for (int i = 0; i < cols; i++)
            {
                this.gridDisplay.ColumnDefinitions.Add(new ColumnDefinition()
                {
                    Width = new GridLength(1, GridUnitType.Star)
                });
                this.columnWidthGrid.ColumnDefinitions.Add(new ColumnDefinition()
                {
                    Width = new GridLength(1, GridUnitType.Star)
                });
                GridLengthEditor editor = new GridLengthEditor(Orientation.Horizontal, i, (colDefitions.Elements().ElementAt(i).Attribute("Width") ?? new XAttribute("Width", "")).Value);

                editor.SelectedValueChanged += new EventHandler <GridLengthSelectionChangedEventArgs>(EditorSelectedValueChanged);
                editor.Deleted             += new EventHandler <GridLengthSelectionChangedEventArgs>(EditorDeleted);
                editor.MouseLeftButtonDown += new MouseButtonEventHandler(EditorMouseLeftButtonDown);
                editor.Drop     += new DragEventHandler(EditorDrop);
                editor.DragOver += new DragEventHandler(EditorDragOver);

                editor.AllowDrop = true;

                this.columnWidthGrid.Children.Add(editor);

                Button leftAddButton = new Button()
                {
                    Content             = "+",
                    HorizontalAlignment = HorizontalAlignment.Left,
                    Margin  = new Thickness(-10, 10, 5, 10),
                    Padding = new Thickness(3),
                    Tag     = i
                };

                leftAddButton.Click += BtnAddColumnClick;

                leftAddButton.SetValue(Grid.ColumnProperty, i);
                this.columnWidthGrid.Children.Add(leftAddButton);

                if (cols == i + 1)
                {
                    Button rightAddButton = new Button()
                    {
                        Content             = "+",
                        HorizontalAlignment = HorizontalAlignment.Right,
                        Margin  = new Thickness(5, 10, 0, 10),
                        Padding = new Thickness(3)
                    };

                    rightAddButton.Click += BtnAddColumnClick;

                    rightAddButton.SetValue(Grid.ColumnProperty, i);
                    this.columnWidthGrid.Children.Add(rightAddButton);
                }
            }

            for (int i = 0; i < rows; i++)
            {
                this.gridDisplay.RowDefinitions.Add(new RowDefinition()
                {
                    Height = new GridLength(1, GridUnitType.Star)
                });

                this.rowHeightGrid.RowDefinitions.Add(new RowDefinition()
                {
                    Height = new GridLength(1, GridUnitType.Star)
                });
                GridLengthEditor editor = new GridLengthEditor(Orientation.Vertical, i, (rowDefitions.Elements().ElementAt(i).Attribute("Height") ?? new XAttribute("Height", "")).Value);

                editor.SelectedValueChanged += new EventHandler <GridLengthSelectionChangedEventArgs>(EditorSelectedValueChanged);
                editor.Deleted             += new EventHandler <GridLengthSelectionChangedEventArgs>(EditorDeleted);
                editor.MouseLeftButtonDown += new MouseButtonEventHandler(EditorMouseLeftButtonDown);
                editor.Drop     += new DragEventHandler(EditorDrop);
                editor.DragOver += new DragEventHandler(EditorDragOver);

                editor.AllowDrop = true;

                this.rowHeightGrid.Children.Add(editor);

                Button topAddButton = new Button()
                {
                    Content           = "+",
                    VerticalAlignment = VerticalAlignment.Top,
                    Margin            = new Thickness(10, -10, 10, 5),
                    Padding           = new Thickness(3),
                    Tag = i
                };

                topAddButton.Click += BtnAddRowClick;

                topAddButton.SetValue(Grid.RowProperty, i);
                this.rowHeightGrid.Children.Add(topAddButton);

                if (rows == i + 1)
                {
                    Button bottomAddButton = new Button()
                    {
                        Content           = "+",
                        VerticalAlignment = VerticalAlignment.Bottom,
                        Margin            = new Thickness(10, 5, 10, 0),
                        Padding           = new Thickness(3)
                    };

                    bottomAddButton.Click += BtnAddRowClick;

                    bottomAddButton.SetValue(Grid.RowProperty, i);
                    this.rowHeightGrid.Children.Add(bottomAddButton);
                }

                for (int j = 0; j < cols; j++)
                {
                    StackPanel displayRect = new StackPanel()
                    {
                        Margin      = new Thickness(5),
                        Background  = Brushes.LightGray,
                        Orientation = Orientation.Vertical
                    };

                    displayRect.AllowDrop = true;

                    displayRect.Drop     += new DragEventHandler(DisplayRectDrop);
                    displayRect.DragOver += new DragEventHandler(DisplayRectDragOver);

                    displayRect.Children.AddRange(BuildItemsForCell(i, j));

                    displayRect.SetValue(Grid.RowProperty, i);
                    displayRect.SetValue(Grid.ColumnProperty, j);

                    displayRect.ContextMenuOpening += new ContextMenuEventHandler(DisplayRectContextMenuOpening);

                    this.gridDisplay.Children.Add(displayRect);
                }
            }

            this.InvalidateVisual();
        }