Пример #1
0
 private void CreateButtons(string[] buttons)
 {
     if (buttonsPanel != null)
     {
         foreach (string name in buttons)
         {
             var btn = new Project1UIButton();
             btn.Content = name;
             btn.Margin  = new Thickness(10, 0, 0, 0);
             btn.Click  += (e, c) =>
             {
                 IsButtonClicked = true;
                 OnButtonClick?.Invoke(name, this);
             };
             if ((buttons.Last() != name))
             {
                 btn.Style = (Style)this.FindResource("basic");
             }
             buttonsPanel.Children.Add(btn);
         }
     }
 }
Пример #2
0
        public void ImportElements(List <ElementModel> elements)
        {
            if (elements != null)
            {
                foreach (var element in elements)
                {
                    var item         = new Project1UIDesignItem();
                    var itemModel    = item.DataContext as DesignItemModel;
                    var controlPoint = item.RenderTransform as TranslateTransform;
                    //分配共用的属性
                    itemModel.Width   = element.Width;
                    itemModel.Height  = element.Height;
                    itemModel.Opacity = element.Opacity;
                    controlPoint.X    = element.X;
                    controlPoint.Y    = element.Y;
                    //分配独有的属性
                    switch (element.Type)
                    {
                    case DesignItemType.Button:
                        var button = new Project1UIButton();
                        if (element.Style != null)
                        {
                            var res = this.TryFindResource(element.Style);
                            if (res != null)
                            {
                                button.Style = res as Style;
                            }
                            else
                            {
                                res = this.TryFindResource("default");
                                if (res != null)
                                {
                                    button.Style = res as Style;
                                }
                            }
                        }
                        itemModel.ButtonText      = element.Text;
                        itemModel.ButtonStyleName = element.Style;
                        itemModel.IsFontBold      = element.IsTextBold;
                        itemModel.FontSize        = element.FontSize;
                        itemModel.Command         = element.Command;
                        item.Content = button;
                        break;

                    case DesignItemType.Image:
                        var image = new Image();
                        itemModel.Image = element.Image;
                        item.Content    = image;
                        break;

                    case DesignItemType.Text:
                        var text = new TextBlock();
                        itemModel.Text       = element.Text;
                        itemModel.TextColor  = element.TextColor;
                        itemModel.IsFontBold = element.IsTextBold;
                        itemModel.FontSize   = element.FontSize;
                        item.Content         = text;
                        break;
                    }
                    //将生成的元素加入设计容器界面
                    HandleItem(item);
                    this.Children.Add(item);
                }
            }
        }
Пример #3
0
        private ContextMenu CreateContextMenu()
        {
            var menu    = new ContextMenu();
            var itemAdd = new MenuItem();

            itemAdd.Header = "添加元素";
            //图片
            var itemAddImage = new MenuItem();

            itemAddImage.Header = "图片";
            itemAddImage.Click += (s, e) =>
            {
                var item = new Project1UIDesignItem();
                var data = item.DataContext as DesignItemModel;
                data.Image  = "pack://application:,,,/Project1.UI;component/Assets/Images/sunglasses.png";
                data.Width  = 100;
                data.Height = 100;
                var image = new Image();
                //image.Source = new BitmapImage(new Uri("pack://application:,,,/Project1.UI;component/Assets/Images/sunglasses.png", UriKind.RelativeOrAbsolute));
                item.Content = image;
                HandleItem(item);
                this.Children.Add(item);
            };
            //按钮
            var itemAddButton = new MenuItem();

            itemAddButton.Header = "按钮";
            itemAddButton.Click += (s, e) =>
            {
                var item = new Project1UIDesignItem();
                var data = item.DataContext as DesignItemModel;
                data.ButtonText = "按钮";
                data.Width      = 100;
                data.Height     = 25;
                var element = new Project1UIButton();

                item.Content = element;
                HandleItem(item);
                this.Children.Add(item);
            };
            //文本
            var itemAddText = new MenuItem();

            itemAddText.Header = "文本";
            itemAddText.Click += (s, e) =>
            {
                var item = new Project1UIDesignItem();
                var data = item.DataContext as DesignItemModel;
                data.Text   = "文本";
                data.Width  = 100;
                data.Height = 25;
                var element = new TextBlock();
                element.TextWrapping = TextWrapping.Wrap;
                item.Content         = element;
                HandleItem(item);
                this.Children.Add(item);
            };
            //属性编辑
            var itemAttr = new MenuItem();

            itemAttr.Header = "属性";
            itemAttr.Click += (s, e) =>
            {
                isContextMenuOpen = true;
                attrPopup.IsOpen  = true;
            };
            itemAdd.Items.Add(itemAddText);
            itemAdd.Items.Add(itemAddImage);
            itemAdd.Items.Add(itemAddButton);

            menu.Items.Add(itemAdd);
            menu.Items.Add(itemAttr);


            return(menu);
        }