private void RestoreLayoutView(ITemplate template) { ClearDesignerWorkspace(); if (template == null) { return; } //set active template and create a control to represent it. TemplateUserControl templateLayout = template.GenerateLayout() as TemplateUserControl; if (templateLayout != null) { //measure, size, and arrage the element templateLayout.Width = template.Width; templateLayout.Height = template.Height; templateLayout.Measure(new Size(canvasWorkspace.ActualWidth, canvasWorkspace.ActualHeight)); templateLayout.Arrange(new Rect( (canvasWorkspace.ActualWidth / 2) - (templateLayout.ActualWidth / 2), (canvasWorkspace.ActualHeight / 2) - (templateLayout.ActualHeight / 2), templateLayout.Width, templateLayout.Height)); templateLayout.UpdateLayout(); this.canvasWorkspace.Children.Add(templateLayout); templateLayout.PreviewMouseDown += elementsMouseClick; } }
/// <summary> /// Creates a control heirarchy that displays all elements /// of this template as a WPF control. /// </summary> /// <returns></returns> public virtual UIElement GenerateLayout() { //setup this element's layout TemplateUserControl control = new TemplateUserControl(this); BindLayoutToEditProps(control); BindModelHelper(control.layoutsurfaceContent, "BackgroundColor", TemplateUserControl.BackgroundProperty); //TODO: add parent elements here //now attach child elements foreach (BaseElement elm in _Children) { if (elm != null) { control.LayoutSurface.Children.Add(elm.GenerateLayout()); } } LastLayoutInstance = control; return(control); }
/// <summary> /// Unlocks all previous locked elements. /// </summary> /// <param name="parameter"></param> private void UnlockElements(object parameter) { if (ActiveTemplate == null || ActiveTemplate.LastLayoutInstance == null) { return; } TemplateUserControl templateControl = ActiveTemplate.LastLayoutInstance as TemplateUserControl; if (templateControl != null) { templateControl.CanManipulate = true; foreach (UIElement element in templateControl.layoutsurfaceContent.Children) { ElementUserControl elm = element as ElementUserControl; if (elm != null) { elm.Creator.Locked = false; elm.CanManipulate = true; } } } }
private void elementsMouseClick(object sender, MouseEventArgs e) { Keyboard.ClearFocus(); WorkspaceMousePosition = e.GetPosition(canvasWorkspace); TemplateMousePosition = e.GetPosition((UIElement)sender); HitList.Clear(); VisualTreeHelper.HitTest(canvasWorkspace, null, new HitTestResultCallback(OnHitResults), new PointHitTestParameters(WorkspaceMousePosition)); UserControl parent = null; if (HitList.Count > 0) { foreach (FrameworkElement result in HitList) { if (result.Name.Equals(TemplateUserControl.LayoutSurfaceId)) { ContentControl content = result.Parent as ContentControl; parent = content.Parent as UserControl; //check if is element and can be manipulated, if not pass right through it if (parent is ElementUserControl && !((ElementUserControl)parent).CanManipulate) { parent = null; continue; } break; } } } if (SelectedDecorator != null) { SelectedDecorator.ShowDecorator = false; } SelectedElement = null; //find out what we hit and hi-light it if applicable. if (parent != null) { TemplateUserControl temp = parent as TemplateUserControl; if (temp == null) { ElementUserControl elm = parent as ElementUserControl; if (elm != null) { if (!elm.CanManipulate) { return; } SelectedElement = elm.Creator; SelectedDecorator = elm; } } else { if (!temp.CanManipulate) { return; } SelectedElement = temp.Creator; SelectedDecorator = temp; } SelectedDecorator.ShowDecorator = true; } }