protected override void OnInitialized()
        {
            base.OnInitialized();
            if (this.ExtendedItem.Parent != null)
            {
                if (this.ExtendedItem.Parent.ComponentType == typeof(Canvas))
                {
                    FrameworkElement extendedControl = (FrameworkElement)this.ExtendedItem.Component;
                    AdornerPanel     adornerPanel    = new AdornerPanel();

                    // If the Element is rotated/skewed in the grid, then margin handles do not appear
                    if (extendedControl.LayoutTransform.Value == Matrix.Identity && extendedControl.RenderTransform.Value == Matrix.Identity)
                    {
                        _canvas  = this.ExtendedItem.Parent.View as Canvas;
                        _handles = new[]
                        {
                            _leftHandle   = new CanvasPositionHandle(ExtendedItem, adornerPanel, HandleOrientation.Left),
                            _topHandle    = new CanvasPositionHandle(ExtendedItem, adornerPanel, HandleOrientation.Top),
                            _rightHandle  = new CanvasPositionHandle(ExtendedItem, adornerPanel, HandleOrientation.Right),
                            _bottomHandle = new CanvasPositionHandle(ExtendedItem, adornerPanel, HandleOrientation.Bottom),
                        };
                    }

                    if (adornerPanel != null)
                    {
                        this.Adorners.Add(adornerPanel);
                    }
                }
            }
        }
Exemple #2
0
        protected override void OnInitialized()
        {
            base.OnInitialized();
            if (this.ExtendedItem.Parent != null)
            {
                if (this.ExtendedItem.Parent.ComponentType == typeof(Grid))
                {
                    FrameworkElement extendedControl = (FrameworkElement)this.ExtendedItem.Component;
                    AdornerPanel     adornerPanel    = new AdornerPanel();

                    // If the Element is rotated/skewed in the grid, then margin handles do not appear
                    if (extendedControl.LayoutTransform.Value == Matrix.Identity && extendedControl.RenderTransform.Value == Matrix.Identity)
                    {
                        _grid    = this.ExtendedItem.Parent.View as Grid;
                        _handles = new[]
                        {
                            _leftHandle   = new MarginHandle(ExtendedItem, adornerPanel, HandleOrientation.Left),
                            _topHandle    = new MarginHandle(ExtendedItem, adornerPanel, HandleOrientation.Top),
                            _rightHandle  = new MarginHandle(ExtendedItem, adornerPanel, HandleOrientation.Right),
                            _bottomHandle = new MarginHandle(ExtendedItem, adornerPanel, HandleOrientation.Bottom),
                        };
                        foreach (var handle in _handles)
                        {
                            handle.MouseLeftButtonDown             += OnMouseDown;
                            handle.Stub.PreviewMouseLeftButtonDown += OnMouseDown;
                        }
                    }

                    if (adornerPanel != null)
                    {
                        this.Adorners.Add(adornerPanel);
                    }
                }
            }
        }
Exemple #3
0
        public void HandleLength()
        {
            Intialize();
            var handle = new MarginHandle(_button, new AdornerPanel(), HandleOrientation.Left);

            Assert.AreEqual(21, handle.HandleLength);
            Assert.IsTrue(handle.Visibility == Visibility.Visible, "Handle is not visible");
            Assert.IsTrue(handle.Stub.Visibility == Visibility.Hidden, "Stub is Visible");
        }
Exemple #4
0
        public void ChangeVerticalAlignment()
        {
            var topHandle    = new MarginHandle(_button, new AdornerPanel(), HandleOrientation.Top);
            var bottomHandle = new MarginHandle(_button, new AdornerPanel(), HandleOrientation.Bottom);

            _button.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Bottom);

            Assert.IsFalse(topHandle.Visibility == Visibility.Visible, "Top Handle is visible");
            Assert.IsFalse(topHandle.Stub.Visibility == Visibility.Hidden, "Top Stub is not Visible");

            Assert.IsTrue(bottomHandle.Visibility == Visibility.Visible, "Bottom Handle is not visible");
            Assert.IsTrue(bottomHandle.Stub.Visibility == Visibility.Hidden, "Bottom Stub is Visible");
        }
Exemple #5
0
        public void ChangeHorizontalAlignment()
        {
            var leftHandle  = new MarginHandle(_button, new AdornerPanel(), HandleOrientation.Left);
            var rightHandle = new MarginHandle(_button, new AdornerPanel(), HandleOrientation.Right);

            _button.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Right);

            Assert.IsFalse(leftHandle.Visibility == Visibility.Visible, "Left Handle is visible");
            Assert.IsFalse(leftHandle.Stub.Visibility == Visibility.Hidden, "Left Stub is not Visible");

            Assert.IsTrue(rightHandle.Visibility == Visibility.Visible, "Right Handle is not visible");
            Assert.IsTrue(rightHandle.Stub.Visibility == Visibility.Hidden, "Right Stub is Visible");
        }
Exemple #6
0
        public void ResizeTest(double width, double height)
        {
            var handle = new MarginHandle(_button, new AdornerPanel(), HandleOrientation.Left);
            var grid   = _button.Parent.View;

            grid.Measure(grid.DesiredSize);
            grid.Arrange(new Rect(grid.DesiredSize));
            grid.UpdateLayout();

            PlacementTests.Resize(new Rect(20, 20, width, height), _button);
            Assert.AreEqual(20, handle.HandleLength);
            Assert.IsTrue(handle.Visibility == Visibility.Visible, "Handle is not visible");
            Assert.IsTrue(handle.Stub.Visibility == Visibility.Hidden, "Stub is Visible");
        }
Exemple #7
0
        public void MoveTest()
        {
            Intialize();
            var handle = new MarginHandle(_button, new AdornerPanel(), HandleOrientation.Left);
            var grid   = _button.Parent.View;

            grid.Measure(grid.DesiredSize);
            grid.Arrange(new Rect(grid.DesiredSize));
            grid.UpdateLayout();

            PlacementTests.Move(new Vector(20, 20), _button);
            Assert.AreEqual(41, handle.HandleLength);
            Assert.IsTrue(handle.Visibility == Visibility.Visible, "Handle is not visible");
            Assert.IsTrue(handle.Stub.Visibility == Visibility.Hidden, "Stub is Visible");
        }
Exemple #8
0
        private void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            var row     = (int)this.ExtendedItem.Properties.GetAttachedProperty(Grid.RowProperty).ValueOnInstance;
            var rowSpan = (int)this.ExtendedItem.Properties.GetAttachedProperty(Grid.RowSpanProperty).ValueOnInstance;

            var column     = (int)this.ExtendedItem.Properties.GetAttachedProperty(Grid.ColumnProperty).ValueOnInstance;
            var columnSpan = (int)this.ExtendedItem.Properties.GetAttachedProperty(Grid.ColumnSpanProperty).ValueOnInstance;

            var margin = (Thickness)this.ExtendedItem.Properties[FrameworkElement.MarginProperty].ValueOnInstance;

            var          point    = this.ExtendedItem.View.TranslatePoint(new Point(), _grid);
            var          position = new Rect(point, this.ExtendedItem.View.RenderSize);
            MarginHandle handle   = null;

            if (sender is MarginHandle)
            {
                handle = sender as MarginHandle;
            }
            if (sender is MarginStub)
            {
                handle = ((MarginStub)sender).Handle;
            }
            if (handle != null)
            {
                switch (handle.Orientation)
                {
                case HandleOrientation.Left:
                    if (_rightHandle.Visibility == Visibility.Visible)
                    {
                        if (_leftHandle.Visibility == Visibility.Visible)
                        {
                            margin.Left = 0;
                            this.ExtendedItem.Properties[FrameworkElement.WidthProperty].SetValue(position.Width);
                            this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Right);
                        }
                        else
                        {
                            var leftMargin = position.Left - GetColumnOffset(column);
                            margin.Left = leftMargin;
                            this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].Reset();
                        }
                    }
                    else
                    {
                        if (_leftHandle.Visibility == Visibility.Visible)
                        {
                            margin.Left = 0;
                            var rightMargin = GetColumnOffset(column + columnSpan) - position.Right;
                            margin.Right = rightMargin;

                            this.ExtendedItem.Properties[FrameworkElement.WidthProperty].SetValue(position.Width);
                            this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Right);
                        }
                        else
                        {
                            var leftMargin = position.Left - GetColumnOffset(column);
                            margin.Left = leftMargin;
                            this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Left);
                        }
                    }
                    break;

                case HandleOrientation.Top:
                    if (_bottomHandle.Visibility == Visibility.Visible)
                    {
                        if (_topHandle.Visibility == Visibility.Visible)
                        {
                            margin.Top = 0;
                            this.ExtendedItem.Properties[FrameworkElement.HeightProperty].SetValue(position.Height);
                            this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Bottom);
                        }
                        else
                        {
                            var topMargin = position.Top - GetRowOffset(row);
                            margin.Top = topMargin;
                            this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].Reset();
                        }
                    }
                    else
                    {
                        if (_topHandle.Visibility == Visibility.Visible)
                        {
                            margin.Top = 0;
                            var bottomMargin = GetRowOffset(row + rowSpan) - position.Bottom;
                            margin.Bottom = bottomMargin;

                            this.ExtendedItem.Properties[FrameworkElement.HeightProperty].SetValue(position.Height);
                            this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Bottom);
                        }
                        else
                        {
                            var topMargin = position.Top - GetRowOffset(row);
                            margin.Top = topMargin;
                            this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Top);
                        }
                    }
                    break;

                case HandleOrientation.Right:
                    if (_leftHandle.Visibility == Visibility.Visible)
                    {
                        if (_rightHandle.Visibility == Visibility.Visible)
                        {
                            margin.Right = 0;
                            this.ExtendedItem.Properties[FrameworkElement.WidthProperty].SetValue(position.Width);
                            this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Left);
                        }
                        else
                        {
                            var rightMargin = GetColumnOffset(column + columnSpan) - position.Right;
                            margin.Right = rightMargin;
                            this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].Reset();
                        }
                    }
                    else
                    {
                        if (_rightHandle.Visibility == Visibility.Visible)
                        {
                            margin.Right = 0;
                            var leftMargin = position.Left - GetColumnOffset(column);
                            margin.Left = leftMargin;

                            this.ExtendedItem.Properties[FrameworkElement.WidthProperty].SetValue(position.Width);
                            this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Left);
                        }
                        else
                        {
                            var rightMargin = GetColumnOffset(column + columnSpan) - position.Right;
                            margin.Right = rightMargin;
                            this.ExtendedItem.Properties[FrameworkElement.HorizontalAlignmentProperty].SetValue(HorizontalAlignment.Right);
                        }
                    }
                    break;

                case HandleOrientation.Bottom:
                    if (_topHandle.Visibility == Visibility.Visible)
                    {
                        if (_bottomHandle.Visibility == Visibility.Visible)
                        {
                            margin.Bottom = 0;
                            this.ExtendedItem.Properties[FrameworkElement.HeightProperty].SetValue(position.Height);
                            this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Top);
                        }
                        else
                        {
                            var bottomMargin = GetRowOffset(row + rowSpan) - position.Bottom;
                            margin.Bottom = bottomMargin;
                            this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].Reset();
                        }
                    }
                    else
                    {
                        if (_bottomHandle.Visibility == Visibility.Visible)
                        {
                            margin.Bottom = 0;
                            var topMargin = position.Top - GetRowOffset(row);
                            margin.Top = topMargin;

                            this.ExtendedItem.Properties[FrameworkElement.HeightProperty].SetValue(position.Height);
                            this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Top);
                        }
                        else
                        {
                            var bottomMargin = GetRowOffset(row + rowSpan) - position.Bottom;
                            margin.Bottom = bottomMargin;
                            this.ExtendedItem.Properties[FrameworkElement.VerticalAlignmentProperty].SetValue(VerticalAlignment.Bottom);
                        }
                    }
                    break;
                }
            }
            this.ExtendedItem.Properties[FrameworkElement.MarginProperty].SetValue(margin);
        }