예제 #1
0
        public void UndoRedoChangeGroupTest()
        {
            DesignItem  button = CreateCanvasContext("<Button/>");
            UndoService s      = button.Context.Services.GetService <UndoService>();

            Assert.IsFalse(s.CanUndo);
            Assert.IsFalse(s.CanRedo);
            using (ChangeGroup g = button.OpenGroup("Resize")) {
                button.Properties["Width"].SetValue(100.0);
                button.Properties["Height"].SetValue(200.0);
                g.Commit();
            }
            Assert.IsTrue(s.CanUndo);
            Assert.IsFalse(s.CanRedo);
            AssertCanvasDesignerOutput(@"<Button Width=""100"" Height=""200"" />", button.Context);
            s.Undo();
            Assert.IsFalse(s.CanUndo);
            Assert.IsTrue(s.CanRedo);
            AssertCanvasDesignerOutput(@"<Button />", button.Context);
            s.Redo();
            Assert.IsTrue(s.CanUndo);
            Assert.IsFalse(s.CanRedo);
            AssertCanvasDesignerOutput(@"<Button Width=""100"" Height=""200"" />", button.Context);
            AssertLog("");
        }
예제 #2
0
        void Click_EditStyle(object sender, RoutedEventArgs e)
        {
            var cg = designItem.OpenGroup("Edit Style");

            var    element         = designItem.View;
            object defaultStyleKey = element.GetValue(FrameworkElement.DefaultStyleKeyProperty);
            Style  style           = Application.Current.TryFindResource(defaultStyleKey) as Style;

            var service = ((XamlComponentService)designItem.Services.Component);

            var           ms     = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(ms, System.Text.Encoding.UTF8);

            writer.Formatting = Formatting.Indented;
            XamlWriter.Save(style, writer);

            var rootItem = this.designItem.Context.RootItem as XamlDesignItem;

            ms.Position = 0;
            var sr   = new StreamReader(ms);
            var xaml = sr.ReadToEnd();

            var xamlObject = XamlParser.ParseSnippet(rootItem.XamlObject, xaml, ((XamlDesignContext)this.designItem.Context).ParserSettings);

            var styleDesignItem = service.RegisterXamlComponentRecursive(xamlObject);

            try {
                designItem.Properties.GetProperty("Resources").CollectionElements.Add(styleDesignItem);
                cg.Commit();
            }
            catch (Exception) {
                cg.Abort();
            }
        }
        private void Ok_Click(object sender, RoutedEventArgs e)
        {
            var changeGroup = designItem.OpenGroup("Formated Text");

            designItem.Properties.GetProperty(TextBlock.TextProperty).Reset();

            var inlinesProperty = designItem.Properties.GetProperty("Inlines");

            inlinesProperty.CollectionElements.Clear();

            var doc = richTextBox.Document;

            richTextBox.Document = new FlowDocument();

            var inlines = new List <DesignItem>();

            GetDesignItems(doc.Blocks, inlines);

            foreach (var inline in inlines)
            {
                inlinesProperty.CollectionElements.Add(inline);
            }

            changeGroup.Commit();

            this.TryFindParent <Window>().Close();
        }
예제 #4
0
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var val = value;

                if (_converter != null)
                {
                    val = _converter.ConvertBack(value, targetType, parameter, culture);
                }

                var changeGroup = _designItem.OpenGroup("Property: " + _propertyName);

                try
                {
                    var property = _designItem.Properties.GetProperty(_propertyName);

                    property.SetValue(val);


                    if (!_singleItemProperty && _designItem.Services.Selection.SelectedItems.Count > 1)
                    {
                        var msg = MessageBoxResult.Yes;
                        if (_askWhenMultipleItemsSelected)
                        {
                            msg = MessageBox.Show("Apply changes to all selected Items", "", MessageBoxButton.YesNo);
                        }
                        if (msg == MessageBoxResult.Yes)
                        {
                            foreach (var item in _designItem.Services.Selection.SelectedItems)
                            {
                                try
                                {
                                    if (_property != null)
                                    {
                                        property = item.Properties.GetProperty(_property);
                                    }
                                    else
                                    {
                                        property = item.Properties.GetProperty(_propertyName);
                                    }
                                }
                                catch (Exception)
                                { }
                                if (property != null)
                                {
                                    property.SetValue(val);
                                }
                            }
                        }
                    }

                    changeGroup.Commit();
                }
                catch (Exception)
                {
                    changeGroup.Abort();
                }

                return(val);
            }
        DesignItem CreateItem(DesignContext context, Type componentType)
        {
            object     newInstance = context.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory(componentType, null);
            DesignItem item        = context.Services.Component.RegisterComponentForDesigner(newInstance);

            changeGroup = item.OpenGroup("Draw Polyline");
            context.Services.ExtensionManager.ApplyDefaultInitializers(item);
            return(item);
        }
예제 #6
0
 public void StartEditing()
 {
     if (changeGroup == null)
     {
         changeGroup        = designItem.OpenGroup("Change Text");
         _isChangeGroupOpen = true;
     }
     this.Visibility = Visibility.Visible;
 }
예제 #7
0
        /// <summary>
        /// Is called to create the item used by the CreateComponentTool.
        /// </summary>
        protected virtual DesignItem CreateItem(DesignContext context)
        {
            object     newInstance = context.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory(componentType, null);
            DesignItem item        = context.Services.Component.RegisterComponentForDesigner(newInstance);

            changeGroup = item.OpenGroup("Drop Control");
            context.Services.Component.SetDefaultPropertyValues(item);
            context.Services.ExtensionManager.ApplyDefaultInitializers(item);
            return(item);
        }
예제 #8
0
        private void Ok_Click(object sender, RoutedEventArgs e)
        {
            var changeGroup = designItem.OpenGroup("Formated Text");

            SetTextBlockTextFromRichTextBlox(designItem, richTextBox);

            changeGroup.Commit();

            this.TryFindParent <Window>().Close();
        }
예제 #9
0
 protected override void OnMouseMove(MouseEventArgs e)
 {
     if (mouseIsDown)
     {
         double mousePos = GetCoordinate(e.GetPosition(grid));
         if (activeChangeGroup == null)
         {
             if (Math.Abs(mousePos - mouseStartPos)
                 >= GetCoordinate(new Point(SystemParameters.MinimumHorizontalDragDistance, SystemParameters.MinimumVerticalDragDistance)))
             {
                 activeChangeGroup = gridItem.OpenGroup("Change grid row/column size");
                 RememberOriginalSize();
             }
         }
         if (activeChangeGroup != null)
         {
             ChangeSize(mousePos - mouseStartPos);
         }
     }
 }
예제 #10
0
        void UndoRedoDictionaryInternal(bool useExplicitDictionary)
        {
            const string       originalXaml = "<Button />";
            DesignItem         button       = CreateCanvasContext(originalXaml);
            UndoService        s            = button.Context.Services.GetService <UndoService>();
            IComponentService  component    = button.Context.Services.Component;
            string             expectedXamlWithDictionary;
            DesignItemProperty dictionaryProp;

            Assert.IsFalse(s.CanUndo);
            Assert.IsFalse(s.CanRedo);

            using (ChangeGroup g = button.OpenGroup("UndoRedoDictionaryInternal test")) {
                DesignItem containerItem = component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassContainer());

                dictionaryProp = containerItem.Properties["Dictionary"];

                if (useExplicitDictionary)
                {
                    dictionaryProp.SetValue(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassDictionary());

                    expectedXamlWithDictionary = @"<Button>
  <Button.Tag>
    <Controls0:ExampleClassContainer>
      <Controls0:ExampleClassContainer.Dictionary>
        <Controls0:ExampleClassDictionary>
          <Controls0:ExampleClass x:Key=""testKey"" StringProp=""String value"" />
        </Controls0:ExampleClassDictionary>
      </Controls0:ExampleClassContainer.Dictionary>
    </Controls0:ExampleClassContainer>
  </Button.Tag>
</Button>";
                }
                else
                {
                    expectedXamlWithDictionary = @"<Button>
  <Button.Tag>
    <Controls0:ExampleClassContainer>
      <Controls0:ExampleClassContainer.Dictionary>
        <Controls0:ExampleClass x:Key=""testKey"" StringProp=""String value"" />
      </Controls0:ExampleClassContainer.Dictionary>
    </Controls0:ExampleClassContainer>
  </Button.Tag>
</Button>";
                }

                DesignItem exampleClassItem = component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClass());
                exampleClassItem.Key = "testKey";
                exampleClassItem.Properties["StringProp"].SetValue("String value");
                dictionaryProp.CollectionElements.Add(exampleClassItem);

                button.Properties["Tag"].SetValue(containerItem);
                g.Commit();
            }

            Assert.IsTrue(s.CanUndo);
            Assert.IsFalse(s.CanRedo);
            AssertCanvasDesignerOutput(expectedXamlWithDictionary, button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");

            dictionaryProp = button.Properties["Tag"].Value.Properties["Dictionary"];
            Assert.IsTrue(((ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassDictionary)dictionaryProp.ValueOnInstance).Count == dictionaryProp.CollectionElements.Count);

            s.Undo();
            Assert.IsFalse(s.CanUndo);
            Assert.IsTrue(s.CanRedo);
            AssertCanvasDesignerOutput(originalXaml, button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");

            s.Redo();
            Assert.IsTrue(s.CanUndo);
            Assert.IsFalse(s.CanRedo);
            AssertCanvasDesignerOutput(expectedXamlWithDictionary, button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");

            dictionaryProp = button.Properties["Tag"].Value.Properties["Dictionary"];
            Assert.IsTrue(((ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassDictionary)dictionaryProp.ValueOnInstance).Count == dictionaryProp.CollectionElements.Count);

            AssertLog("");
        }
예제 #11
0
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            e.Handled = true;
            Focus();
            adornerPanel.Children.Remove(previewAdorner);
            if (orientation == Orientation.Vertical)
            {
                double             insertionPosition = e.GetPosition(this).Y;
                DesignItemProperty rowCollection     = gridItem.Properties["RowDefinitions"];

                DesignItem currentRow = null;

                using (ChangeGroup changeGroup = gridItem.OpenGroup("Split grid row")) {
                    if (rowCollection.CollectionElements.Count == 0)
                    {
                        DesignItem firstRow = gridItem.Services.Component.RegisterComponentForDesigner(new RowDefinition());
                        rowCollection.CollectionElements.Add(firstRow);
                        grid.UpdateLayout();                         // let WPF assign firstRow.ActualHeight

                        currentRow = firstRow;
                    }
                    else
                    {
                        RowDefinition current = grid.RowDefinitions
                                                .FirstOrDefault(r => insertionPosition >= r.Offset &&
                                                                insertionPosition <= (r.Offset + r.ActualHeight));
                        if (current != null)
                        {
                            currentRow = gridItem.Services.Component.GetDesignItem(current);
                        }
                    }

                    if (currentRow == null)
                    {
                        currentRow = gridItem.Services.Component.GetDesignItem(grid.RowDefinitions.Last());
                    }

                    unitSelector.SelectedItem = currentRow;
                    for (int i = 0; i < grid.RowDefinitions.Count; i++)
                    {
                        RowDefinition row = grid.RowDefinitions[i];
                        if (row.Offset > insertionPosition)
                        {
                            continue;
                        }
                        if (row.Offset + row.ActualHeight < insertionPosition)
                        {
                            continue;
                        }

                        // split row
                        GridLength oldLength = (GridLength)row.GetValue(RowDefinition.HeightProperty);
                        GridLength newLength1, newLength2;
                        SplitLength(oldLength, insertionPosition - row.Offset, row.ActualHeight, out newLength1, out newLength2);
                        DesignItem newRowDefinition = gridItem.Services.Component.RegisterComponentForDesigner(new RowDefinition());
                        rowCollection.CollectionElements.Insert(i + 1, newRowDefinition);
                        rowCollection.CollectionElements[i].Properties[RowDefinition.HeightProperty].SetValue(newLength1);
                        newRowDefinition.Properties[RowDefinition.HeightProperty].SetValue(newLength2);
                        grid.UpdateLayout();
                        FixIndicesAfterSplit(i, Grid.RowProperty, Grid.RowSpanProperty, insertionPosition);
                        grid.UpdateLayout();
                        changeGroup.Commit();
                        break;
                    }
                }
            }
            else
            {
                double             insertionPosition = e.GetPosition(this).X;
                DesignItemProperty columnCollection  = gridItem.Properties["ColumnDefinitions"];

                DesignItem currentColumn = null;

                using (ChangeGroup changeGroup = gridItem.OpenGroup("Split grid column")) {
                    if (columnCollection.CollectionElements.Count == 0)
                    {
                        DesignItem firstColumn = gridItem.Services.Component.RegisterComponentForDesigner(new ColumnDefinition());
                        columnCollection.CollectionElements.Add(firstColumn);
                        grid.UpdateLayout();                         // let WPF assign firstColumn.ActualWidth

                        currentColumn = firstColumn;
                    }
                    else
                    {
                        ColumnDefinition current = grid.ColumnDefinitions
                                                   .FirstOrDefault(r => insertionPosition >= r.Offset &&
                                                                   insertionPosition <= (r.Offset + r.ActualWidth));
                        if (current != null)
                        {
                            currentColumn = gridItem.Services.Component.GetDesignItem(current);
                        }
                    }

                    if (currentColumn == null)
                    {
                        currentColumn = gridItem.Services.Component.GetDesignItem(grid.ColumnDefinitions.Last());
                    }

                    unitSelector.SelectedItem = currentColumn;
                    for (int i = 0; i < grid.ColumnDefinitions.Count; i++)
                    {
                        ColumnDefinition column = grid.ColumnDefinitions[i];
                        if (column.Offset > insertionPosition)
                        {
                            continue;
                        }
                        if (column.Offset + column.ActualWidth < insertionPosition)
                        {
                            continue;
                        }

                        // split column
                        GridLength oldLength = (GridLength)column.GetValue(ColumnDefinition.WidthProperty);
                        GridLength newLength1, newLength2;
                        SplitLength(oldLength, insertionPosition - column.Offset, column.ActualWidth, out newLength1, out newLength2);
                        DesignItem newColumnDefinition = gridItem.Services.Component.RegisterComponentForDesigner(new ColumnDefinition());
                        columnCollection.CollectionElements.Insert(i + 1, newColumnDefinition);
                        columnCollection.CollectionElements[i].Properties[ColumnDefinition.WidthProperty].SetValue(newLength1);
                        newColumnDefinition.Properties[ColumnDefinition.WidthProperty].SetValue(newLength2);
                        grid.UpdateLayout();
                        FixIndicesAfterSplit(i, Grid.ColumnProperty, Grid.ColumnSpanProperty, insertionPosition);
                        changeGroup.Commit();
                        grid.UpdateLayout();
                        break;
                    }
                }
            }
            InvalidateVisual();
        }
예제 #12
0
        void UndoRedoListInternal(bool useExplicitList)
        {
            DesignItem         button    = CreateCanvasContext("<Button/>");
            UndoService        s         = button.Context.Services.GetService <UndoService>();
            IComponentService  component = button.Context.Services.Component;
            string             expectedXamlWithList;
            DesignItemProperty otherListProp;

            Assert.IsFalse(s.CanUndo);
            Assert.IsFalse(s.CanRedo);

            using (ChangeGroup g = button.OpenGroup("UndoRedoListInternal test")) {
                DesignItem containerItem = component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassContainer());

                otherListProp = containerItem.Properties["OtherList"];

                if (useExplicitList)
                {
                    otherListProp.SetValue(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassList());

                    expectedXamlWithList = @"<Button>
  <Button.Tag>
    <Controls0:ExampleClassContainer>
      <Controls0:ExampleClassContainer.OtherList>
        <Controls0:ExampleClassList>
          <Controls0:ExampleClass StringProp=""String value"" />
        </Controls0:ExampleClassList>
      </Controls0:ExampleClassContainer.OtherList>
    </Controls0:ExampleClassContainer>
  </Button.Tag>
</Button>";
                }
                else
                {
                    expectedXamlWithList = @"<Button>
  <Button.Tag>
    <Controls0:ExampleClassContainer>
      <Controls0:ExampleClassContainer.OtherList>
        <Controls0:ExampleClass StringProp=""String value"" />
      </Controls0:ExampleClassContainer.OtherList>
    </Controls0:ExampleClassContainer>
  </Button.Tag>
</Button>";
                }

                DesignItem exampleClassItem = component.RegisterComponentForDesigner(new ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClass());
                exampleClassItem.Properties["StringProp"].SetValue("String value");
                otherListProp.CollectionElements.Add(exampleClassItem);

                button.Properties["Tag"].SetValue(containerItem);
                g.Commit();
            }

            Assert.IsTrue(s.CanUndo);
            Assert.IsFalse(s.CanRedo);
            AssertCanvasDesignerOutput(expectedXamlWithList, button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");

            otherListProp = button.Properties["Tag"].Value.Properties["OtherList"];
            Assert.IsTrue(((ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassList)otherListProp.ValueOnInstance).Count == otherListProp.CollectionElements.Count);

            s.Undo();
            Assert.IsFalse(s.CanUndo);
            Assert.IsTrue(s.CanRedo);
            AssertCanvasDesignerOutput("<Button>\n</Button>", button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");

            s.Redo();
            Assert.IsTrue(s.CanUndo);
            Assert.IsFalse(s.CanRedo);
            AssertCanvasDesignerOutput(expectedXamlWithList, button.Context, "xmlns:Controls0=\"" + ICSharpCode.WpfDesign.Tests.XamlDom.XamlTypeFinderTests.XamlDomTestsNamespace + "\"");

            otherListProp = button.Properties["Tag"].Value.Properties["OtherList"];
            Assert.IsTrue(((ICSharpCode.WpfDesign.Tests.XamlDom.ExampleClassList)otherListProp.ValueOnInstance).Count == otherListProp.CollectionElements.Count);

            AssertLog("");
        }
예제 #13
0
        public static void ApplyTransform(DesignItem designItem, Transform transform, bool relative = true)
        {
            var changeGroup = designItem.OpenGroup("Apply Transform");

            Transform oldTransform = null;

            if (designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).IsSet)
            {
                oldTransform = designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty)
                               .ValueOnInstance as Transform;
            }

            if (oldTransform is MatrixTransform)
            {
                var mt = oldTransform as MatrixTransform;
                var tg = new TransformGroup();
                if (mt.Matrix.OffsetX != 0 && mt.Matrix.OffsetY != 0)
                {
                    tg.Children.Add(new TranslateTransform()
                    {
                        X = mt.Matrix.OffsetX, Y = mt.Matrix.OffsetY
                    });
                }
                if (mt.Matrix.M11 != 0 && mt.Matrix.M22 != 0)
                {
                    tg.Children.Add(new ScaleTransform()
                    {
                        ScaleX = mt.Matrix.M11, ScaleY = mt.Matrix.M22
                    });
                }

                var angle = Math.Atan2(mt.Matrix.M21, mt.Matrix.M11) * 180 / Math.PI;
                if (angle != 0)
                {
                    tg.Children.Add(new RotateTransform()
                    {
                        Angle = angle
                    });
                }
                //if (mt.Matrix.M11 != 0 && mt.Matrix.M22 != 0)
                //	tg.Children.Add(new SkewTransform(){ ScaleX = mt.Matrix.M11, ScaleY = mt.Matrix.M22 });
            }
            else if (oldTransform != null && oldTransform.GetType() != transform.GetType())
            {
                var tg    = new TransformGroup();
                var tgDes = designItem.Services.Component.RegisterComponentForDesigner(tg);
                tgDes.ContentProperty.CollectionElements.Add(designItem.Services.Component.GetDesignItem(oldTransform));
                designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).SetValue(tg);
                oldTransform = tg;
            }


            if (transform is RotateTransform)
            {
                var rotateTransform = transform as RotateTransform;

                if (oldTransform is RotateTransform || oldTransform == null)
                {
                    if (rotateTransform.Angle != 0)
                    {
                        designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).SetValue(transform);
                        var angle = rotateTransform.Angle;
                        if (relative && oldTransform != null)
                        {
                            angle = rotateTransform.Angle + ((RotateTransform)oldTransform).Angle;
                        }
                        designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty)
                        .Value.Properties.GetProperty(RotateTransform.AngleProperty)
                        .SetValue(angle);
                        if (rotateTransform.CenterX != 0.0)
                        {
                            designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty)
                            .Value.Properties.GetProperty(RotateTransform.CenterXProperty)
                            .SetValue(rotateTransform.CenterX);
                        }
                        if (rotateTransform.CenterY != 0.0)
                        {
                            designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty)
                            .Value.Properties.GetProperty(RotateTransform.CenterYProperty)
                            .SetValue(rotateTransform.CenterY);
                        }

                        if (oldTransform == null)
                        {
                            designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty)
                            .SetValue(new Point(0.5, 0.5));
                        }
                    }
                    else
                    {
                        designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).Reset();
                        designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty).Reset();
                    }
                }
                else if (oldTransform is TransformGroup)
                {
                    var tg  = oldTransform as TransformGroup;
                    var rot = tg.Children.FirstOrDefault(x => x is RotateTransform);
                    if (rot != null)
                    {
                        designItem.Services.Component.GetDesignItem(tg)
                        .ContentProperty.CollectionElements
                        .Remove(designItem.Services.Component.GetDesignItem(rot));
                    }
                    if (rotateTransform.Angle != 0)
                    {
                        var des = designItem.Services.Component.GetDesignItem(transform);
                        if (des == null)
                        {
                            des = designItem.Services.Component.RegisterComponentForDesigner(transform);
                        }
                        designItem.Services.Component.GetDesignItem(tg).ContentProperty.CollectionElements.Add(des);
                        if (oldTransform == null)
                        {
                            designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty)
                            .SetValue(new Point(0.5, 0.5));
                        }
                    }
                }
                else
                {
                    if (rotateTransform.Angle != 0)
                    {
                        designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).SetValue(transform);
                        if (oldTransform == null)
                        {
                            designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty)
                            .SetValue(new Point(0.5, 0.5));
                        }
                    }
                }
            }

            ((DesignPanel)designItem.Services.DesignPanel).AdornerLayer
            .UpdateAdornersForElement(designItem.View, true);

            changeGroup.Commit();
        }
예제 #14
0
		public static void ApplyTransform(DesignItem designItem, Transform transform, bool relative = true)
		{
			var changeGroup = designItem.OpenGroup("Apply Transform");
			
			Transform oldTransform = null;
			if (designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).IsSet) {
				oldTransform = designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).ValueOnInstance as Transform;
			}
			
			if (oldTransform is MatrixTransform) {
				var mt = oldTransform as MatrixTransform;
				var tg = new TransformGroup();
				if (mt.Matrix.OffsetX != 0 && mt.Matrix.OffsetY != 0)
					tg.Children.Add(new TranslateTransform(){ X = mt.Matrix.OffsetX, Y = mt.Matrix.OffsetY });
				if (mt.Matrix.M11 != 0 && mt.Matrix.M22 != 0)
					tg.Children.Add(new ScaleTransform(){ ScaleX = mt.Matrix.M11, ScaleY = mt.Matrix.M22 });

				var angle = Math.Atan2(mt.Matrix.M21, mt.Matrix.M11) * 180 / Math.PI;
				if (angle != 0)
					tg.Children.Add(new RotateTransform(){ Angle = angle });
				//if (mt.Matrix.M11 != 0 && mt.Matrix.M22 != 0)
				//	tg.Children.Add(new SkewTransform(){ ScaleX = mt.Matrix.M11, ScaleY = mt.Matrix.M22 });
			} else if (oldTransform != null && oldTransform.GetType() != transform.GetType()) {
				var tg = new TransformGroup();
				var tgDes = designItem.Services.Component.RegisterComponentForDesigner(tg);
				tgDes.ContentProperty.CollectionElements.Add(designItem.Services.Component.GetDesignItem(oldTransform));
				designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).SetValue(tg);
				oldTransform = tg;
			}
			
			
			
			if (transform is RotateTransform) {
				var rotateTransform = transform as RotateTransform;
				
				if (oldTransform is RotateTransform || oldTransform == null) {
					if (rotateTransform.Angle != 0) {
						designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).SetValue(transform);
						var angle = rotateTransform.Angle;
						if (relative && oldTransform != null) {
							angle = rotateTransform.Angle + ((RotateTransform)oldTransform).Angle;
						}
						designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).Value.Properties.GetProperty(RotateTransform.AngleProperty).SetValue(angle);
						if (rotateTransform.CenterX != 0.0)
							designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).Value.Properties.GetProperty(RotateTransform.CenterXProperty).SetValue(rotateTransform.CenterX);
						if (rotateTransform.CenterY != 0.0)
							designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).Value.Properties.GetProperty(RotateTransform.CenterYProperty).SetValue(rotateTransform.CenterY);
						
						if (oldTransform == null)
							designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty).SetValue(new Point(0.5, 0.5));
					}
					else {
						designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).Reset();
						designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty).Reset();
					}
				} else if (oldTransform is TransformGroup) {
					var tg = oldTransform as TransformGroup;
					var rot = tg.Children.FirstOrDefault(x=> x is RotateTransform);
					if  (rot != null) {
						designItem.Services.Component.GetDesignItem(tg).ContentProperty.CollectionElements.Remove(designItem.Services.Component.GetDesignItem(rot));
					}
					if (rotateTransform.Angle != 0) {
						var des = designItem.Services.Component.GetDesignItem(transform);
						if (des == null)
							des = designItem.Services.Component.RegisterComponentForDesigner(transform);
						designItem.Services.Component.GetDesignItem(tg).ContentProperty.CollectionElements.Add(des);
						if (oldTransform == null)
							designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty).SetValue(new Point(0.5, 0.5));
					}
				} else {
						if (rotateTransform.Angle != 0) {
							designItem.Properties.GetProperty(FrameworkElement.RenderTransformProperty).SetValue(transform);
							if (oldTransform == null)
								designItem.Properties.GetProperty(FrameworkElement.RenderTransformOriginProperty).SetValue(new Point(0.5, 0.5));
					}
				}
			}
		
			((DesignPanel) designItem.Services.DesignPanel).AdornerLayer.UpdateAdornersForElement(designItem.View, true);
			
			changeGroup.Commit();
		}
예제 #15
0
        public static void WrapItemsNewContainer(IEnumerable <DesignItem> items, Type containerType)
        {
            var collection = items;

            var _context = collection.First().Context as XamlDesignContext;

            var oldContainer = collection.First().Parent;

            if (collection.Any(x => x.Parent != oldContainer))
            {
                return;
            }

            var        newInstance = Activator.CreateInstance(containerType);
            DesignItem newPanel    = _context.Services.Component.RegisterComponentForDesigner(newInstance);
            var        changeGroup = newPanel.OpenGroup("Wrap in Container");

            List <ItemPos> itemList = new List <ItemPos>();

            foreach (var item in collection)
            {
                var itemPos = new ItemPos()
                {
                    DesignItem = item
                };
                itemList.Add(itemPos);

                if (oldContainer.Component is Canvas)
                {
                    var canvas = oldContainer.View as Canvas;

                    if (item.Properties.GetAttachedProperty(Canvas.RightProperty) != null && item.Properties.GetAttachedProperty(Canvas.RightProperty).IsSet)
                    {
                        itemPos.HorizontalAlignment = HorizontalAlignment.Right;
                        itemPos.Xmax = canvas.ActualWidth - (double)item.Properties.GetAttachedProperty(Canvas.RightProperty).ValueOnInstance;
                        itemPos.Xmin = itemPos.Xmax - ((FrameworkElement)item.View).ActualWidth;
                    }
                    else if (item.Properties.GetAttachedProperty(Canvas.LeftProperty) != null && item.Properties.GetAttachedProperty(Canvas.LeftProperty).IsSet)
                    {
                        itemPos.HorizontalAlignment = HorizontalAlignment.Left;
                        itemPos.Xmin = (double)item.Properties.GetAttachedProperty(Canvas.LeftProperty).ValueOnInstance;
                        itemPos.Xmax = itemPos.Xmin + ((FrameworkElement)item.View).ActualWidth;
                    }
                    else
                    {
                        itemPos.HorizontalAlignment = HorizontalAlignment.Left;
                        itemPos.Xmax = itemPos.Xmin + ((FrameworkElement)item.View).ActualWidth;
                    }

                    if (item.Properties.GetAttachedProperty(Canvas.BottomProperty) != null && item.Properties.GetAttachedProperty(Canvas.BottomProperty).IsSet)
                    {
                        itemPos.VerticalAlignment = VerticalAlignment.Bottom;
                        itemPos.Ymax = canvas.ActualHeight - (double)item.Properties.GetAttachedProperty(Canvas.BottomProperty).ValueOnInstance;
                        itemPos.Ymin = itemPos.Ymax - ((FrameworkElement)item.View).ActualHeight;
                    }
                    else if (item.Properties.GetAttachedProperty(Canvas.TopProperty) != null && item.Properties.GetAttachedProperty(Canvas.TopProperty).IsSet)
                    {
                        itemPos.VerticalAlignment = VerticalAlignment.Top;
                        itemPos.Ymin = (double)item.Properties.GetAttachedProperty(Canvas.TopProperty).ValueOnInstance;
                        itemPos.Ymax = itemPos.Ymin + ((FrameworkElement)item.View).ActualHeight;
                    }
                    else
                    {
                        itemPos.VerticalAlignment = VerticalAlignment.Top;
                        itemPos.Ymax = itemPos.Ymin + ((FrameworkElement)item.View).ActualHeight;
                    }

                    item.Properties.GetAttachedProperty(Canvas.RightProperty).Reset();
                    item.Properties.GetAttachedProperty(Canvas.LeftProperty).Reset();
                    item.Properties.GetAttachedProperty(Canvas.TopProperty).Reset();
                    item.Properties.GetAttachedProperty(Canvas.BottomProperty).Reset();
                }
                else if (oldContainer.Component is Grid)
                {
                    var grid = oldContainer.View as Grid;

                    if ((HorizontalAlignment)item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).ValueOnInstance == HorizontalAlignment.Right)
                    {
                        itemPos.HorizontalAlignment = HorizontalAlignment.Right;
                        itemPos.Xmax = grid.ActualWidth - ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Right;
                        itemPos.Xmin = itemPos.Xmax - ((FrameworkElement)item.View).ActualWidth;
                    }
                    else
                    {
                        itemPos.HorizontalAlignment = HorizontalAlignment.Left;
                        itemPos.Xmin = ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Left;
                        itemPos.Xmax = itemPos.Xmin + ((FrameworkElement)item.View).ActualWidth;
                    }

                    if ((VerticalAlignment)item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).ValueOnInstance == VerticalAlignment.Bottom)
                    {
                        itemPos.VerticalAlignment = VerticalAlignment.Bottom;
                        itemPos.Ymax = grid.ActualHeight - ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Bottom;
                        itemPos.Ymin = itemPos.Ymax - ((FrameworkElement)item.View).ActualHeight;
                    }
                    else
                    {
                        itemPos.VerticalAlignment = VerticalAlignment.Top;
                        itemPos.Ymin = ((Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance).Top;
                        itemPos.Ymax = itemPos.Ymin + ((FrameworkElement)item.View).ActualHeight;
                    }

                    item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).Reset();
                    item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).Reset();
                    item.Properties.GetProperty(FrameworkElement.MarginProperty).Reset();
                }

                var parCol = item.ParentProperty.CollectionElements;
                parCol.Remove(item);
            }

            var xmin = itemList.Min(x => x.Xmin);
            var xmax = itemList.Max(x => x.Xmax);
            var ymin = itemList.Min(x => x.Ymin);
            var ymax = itemList.Max(x => x.Ymax);

            if (oldContainer.Component is Canvas)
            {
                newPanel.Properties.GetProperty(FrameworkElement.WidthProperty).SetValue(xmax - xmin);
                newPanel.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(ymax - ymin);
                newPanel.Properties.GetAttachedProperty(Canvas.LeftProperty).SetValue(xmin);
                newPanel.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(ymin);
            }
            else if (oldContainer.Component is Grid)
            {
                newPanel.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).SetValue(HorizontalAlignment.Left);
                newPanel.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Top);
                newPanel.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(new Thickness(xmin, ymin, 0, 0));
                newPanel.Properties.GetProperty(FrameworkElement.WidthProperty).SetValue(xmax - xmin);
                newPanel.Properties.GetProperty(FrameworkElement.HeightProperty).SetValue(ymax - ymin);
            }

            foreach (var item in itemList)
            {
                newPanel.ContentProperty.CollectionElements.Add(item.DesignItem);

                if (newPanel.Component is Canvas)
                {
                    if (item.HorizontalAlignment == HorizontalAlignment.Right)
                    {
                        item.DesignItem.Properties.GetAttachedProperty(Canvas.RightProperty).SetValue(xmax - item.Xmax);
                    }
                    else
                    {
                        item.DesignItem.Properties.GetAttachedProperty(Canvas.LeftProperty).SetValue(item.Xmin - xmin);
                    }

                    if (item.VerticalAlignment == VerticalAlignment.Bottom)
                    {
                        item.DesignItem.Properties.GetAttachedProperty(Canvas.BottomProperty).SetValue(ymax - item.Ymax);
                    }
                    else
                    {
                        item.DesignItem.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(item.Ymin - ymin);
                    }
                }
                else if (newPanel.Component is Grid)
                {
                    Thickness thickness = new Thickness(0);
                    if (item.HorizontalAlignment == HorizontalAlignment.Right)
                    {
                        item.DesignItem.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).SetValue(HorizontalAlignment.Right);
                        thickness.Right = xmax - item.Xmax;
                    }
                    else
                    {
                        item.DesignItem.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).SetValue(HorizontalAlignment.Left);
                        thickness.Left = item.Xmin - xmin;
                    }

                    if (item.VerticalAlignment == VerticalAlignment.Bottom)
                    {
                        item.DesignItem.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Bottom);
                        thickness.Bottom = ymax - item.Ymax;
                    }
                    else
                    {
                        item.DesignItem.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).SetValue(VerticalAlignment.Top);
                        thickness.Top = item.Ymin - ymin;
                    }

                    item.DesignItem.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(thickness);
                }
            }

            oldContainer.ContentProperty.CollectionElements.Add(newPanel);

            changeGroup.Commit();

            _context.Services.Selection.SetSelectedComponents(new [] { newPanel });
        }