/// <summary> /// AddTab menu item action. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event arguments.</param> private void AddTabMenuAction_Execute(object sender, MenuActionEventArgs e) { ModelItem primarySelection = e.Selection.PrimarySelection; ModelItem tabControl = null; if (primarySelection.IsItemOfType(MyPlatformTypes.TabControl.TypeId)) { tabControl = e.Selection.PrimarySelection; } else if (primarySelection.IsItemOfType(MyPlatformTypes.TabItem.TypeId)) { ModelItem parent = e.Selection.PrimarySelection.Parent; if (parent != null && parent.IsItemOfType(MyPlatformTypes.TabControl.TypeId)) { tabControl = parent; } } if (tabControl != null) { using (ModelEditingScope changes = tabControl.BeginEdit(Properties.Resources.TabControl_AddTabMenuItem)) { ModelItem newTabItem = ModelFactory.CreateItem(e.Context, MyPlatformTypes.TabItem.TypeId, CreateOptions.InitializeDefaults); tabControl.Content.Collection.Add(newTabItem); // change selection to newly created TabItem. Selection sel = new Selection(newTabItem); e.Context.Items.SetValue(sel); changes.Complete(); } } }
/// <summary> /// Update the state of the menu items. /// If the Selected Item is a TabControl, a TabItem or /// the contents of a TabItem, then make the menu item visible. /// </summary> /// <param name="sender">Event sender.</param> /// <param name="e">Event argument.</param> private void TabControlContextMenuProvider_UpdateItemStatus(object sender, MenuActionEventArgs e) { // Update AddTab visibility. if (e.Selection.SelectionCount == 1) { ModelItem primarySelection = e.Selection.PrimarySelection; if (primarySelection.IsItemOfType(MyPlatformTypes.TabItem.TypeId)) { // ensure the TabItem is parented to a TabControl. ModelItem parent = primarySelection.Parent; _addTabMenuAction.Visible = (parent != null && parent.IsItemOfType(MyPlatformTypes.TabControl.TypeId)); } else if (primarySelection.IsItemOfType(MyPlatformTypes.TabControl.TypeId)) { _addTabMenuAction.Visible = true; } else { // if it's the TabItem.Content, treat it as the TabItem ModelItem directParent = primarySelection.Parent; if (directParent != null && directParent.IsItemOfType(MyPlatformTypes.TabItem.TypeId)) { ModelItem tabControl = (directParent != null) ? directParent.Parent : null; _addTabMenuAction.Visible = (tabControl != null && tabControl.IsItemOfType(MyPlatformTypes.TabControl.TypeId)); } } } else { _addTabMenuAction.Visible = false; } }
/// <summary> /// Redirect the parent action. /// </summary> /// <param name="parent">The item being dragged in.</param> /// <param name="childType">The type of the item being dragged.</param> /// <returns>The new parent for the child.</returns> public override ModelItem RedirectParent(ModelItem parent, Type childType) { if (parent == null) { throw new ArgumentNullException("parent"); } if (childType == null) { throw new ArgumentNullException("childType"); } if (parent.Content != null && parent.Content.IsSet) { // if Expander has a content and if the content is a panel or a contentcontrol, let the content act as the parent ModelItem content = parent.Content.Value; if (content != null && (content.IsItemOfType(PlatformTypes.Panel.TypeId) || content.IsItemOfType(PlatformTypes.ContentControl.TypeId))) { return(content); } else { _canParent = false; return(parent); // Expander has a content already but its neither Panel nor ContentControl. } } // Expander doesnt have any content and now Expander itself acts as the parent return(base.RedirectParent(parent, childType)); }
/// <summary> /// Called before remove. /// </summary> /// <param name="currentParent">The current parent.</param> /// <param name="newParent">The new parent.</param> /// <param name="child">The child being re-parented.</param> protected virtual void OnBeforeRemove(ModelItem currentParent, ModelItem newParent, ModelItem child) { if (currentParent == null) { throw new ArgumentNullException("currentParent"); } if (newParent == null) { throw new ArgumentNullException("newParent"); } if (child == null) { throw new ArgumentNullException("child"); } // Remove canvas properties if our new parent is not the same kind of panel if (!newParent.IsItemOfType(_targetPanelType)) { foreach (PropertyIdentifier prop in ContainerSpecificProperties) { ModelProperty property = child.Properties.Find(prop); if (property != null) { property.ClearValue(); } } } }
/// <summary> /// Sets BindingPath which will be set to the name of the property that the binding refers to if one exists. /// Discovers Binding.Path.Path using ModelItem access to avoid platform specific types /// </summary> private void SetBindingPath() { string propertyName = string.Empty; //To get this working for DataGridTemplateColumns, we need to be able to serialize designer only variables //which is not available at this time //Get the Binding Property from DataGridBoundColumn ModelProperty bindingProperty = _columnModelItem.Properties.Find(PlatformTypes.DataGridBoundColumn.BindingProperty); if (bindingProperty != null) { //Get the Model for the value of the Binding property ModelItem bindingItem = bindingProperty.Value; //If there is one and its a Binding then get the PropertyPath and extract Path from that if (bindingItem != null && bindingItem.IsItemOfType(PlatformTypes.Binding.TypeId)) { ModelProperty pathProperty = bindingItem.Properties[PlatformTypes.Binding.PathProperty]; if (pathProperty != null) { propertyName = pathProperty.Value.Properties[PlatformTypes.PropertyPath.PathProperty].ComputedValue as string; } } } BindingPath = propertyName; }
/// <summary> /// Called when removing. /// </summary> /// <param name="currentParent">The current parent.</param> /// <param name="newParent">The new parent.</param> /// <param name="child">The child being re-parented.</param> public override void RemoveParent(ModelItem currentParent, ModelItem newParent, ModelItem child) { if (currentParent == null) { throw new ArgumentNullException("currentParent"); } if (newParent == null) { throw new ArgumentNullException("newParent"); } if (child == null) { throw new ArgumentNullException("child"); } if (currentParent.Content.Value == child) { currentParent.Content.ClearValue(); } else { if (currentParent.IsItemOfType(PlatformTypes.HeaderedContentControl.TypeId) && currentParent.Properties[PlatformTypes.HeaderedContentControl.HeaderProperty].Value == child) { currentParent.Properties[PlatformTypes.HeaderedContentControl.HeaderProperty].ClearValue(); // clear the header property. } } }
private ModelItem GetWizard(ModelItem primarySelection) { if (primarySelection.IsItemOfType(Types.Wizard.TypeId)) { return(primarySelection); } return(null); }
/// <summary> /// Returns the string that represents a given DataGridColumn type /// </summary> internal static string GetColumnStringType(ModelItem column) { if (column.IsItemOfType(PlatformTypes.DataGridTextColumn.TypeId)) { return(Properties.Resources.Text_Column); } if (column.IsItemOfType(PlatformTypes.DataGridCheckBoxColumn.TypeId)) { return(Properties.Resources.CheckBox_Column); } if (column.IsItemOfType(PlatformTypes.DataGridTemplateColumn.TypeId)) { return(Properties.Resources.Template_Column); } return(string.Empty); }
/// <summary> /// Determines whether an item may be dragged inside this panel. /// </summary> /// <param name="parent">The panel.</param> /// <param name="childType">The child.</param> /// <returns>True if the item may be parented.</returns> public override bool CanParent(ModelItem parent, Type childType) { if (parent == null) { throw new ArgumentNullException("parent"); } if (childType == null) { throw new ArgumentNullException("childType"); } return(parent.IsItemOfType(_targetPanelType)); }
/// <summary> /// When this TabItem is activated set IsSelected to true. /// </summary> /// <param name="item">A ModelItem representing the adorned element.</param> protected override void Activate(ModelItem item) { if (item != null && item.IsItemOfType(MyPlatformTypes.TabItem.TypeId)) { if (Context != null) { Tool tool = Context.Items.GetValue<Tool>(); if (tool == null || tool.FocusedTask == null) { TabItemDesignModeValueProvider.SetDesignTimeIsSelected(item, true); // activate the tab that has been selected item.View.UpdateLayout(); } } } base.Activate(item); }
/// <summary> /// When this TabItem is activated set IsSelected to true. /// </summary> /// <param name="item">A ModelItem representing the adorned element.</param> protected override void Activate(ModelItem item) { if (item != null && item.IsItemOfType(MyPlatformTypes.TabItem.TypeId)) { if (Context != null) { Tool tool = Context.Items.GetValue <Tool>(); if (tool == null || tool.FocusedTask == null) { TabItemDesignModeValueProvider.SetDesignTimeIsSelected(item, true); // activate the tab that has been selected item.View.UpdateLayout(); } } } base.Activate(item); }
/// <summary> /// Return all TabItems who are visual tree ancestor of the primary selection. /// </summary> /// <param name="selection">The current selection.</param> /// <returns>All TabItems who are visual tree ancestor of primary selection.</returns> protected override IEnumerable <ModelItem> GetPolicyItems(Selection selection) { ModelItem primary = selection.PrimarySelection; List <ModelItem> itemsToReturn = new List <ModelItem>(); if (primary != null) { // travel up the hierarchy to check the control selected is a child of TabItem. for (ModelItem current = primary; current != null; current = current.Parent) { if (current.IsItemOfType(MyPlatformTypes.TabItem.TypeId)) { itemsToReturn.Add(current); } } } return(itemsToReturn); }
/// <summary> /// Returns the string that represents a given DataGridColumn type /// </summary> internal static string GetColumnStringType(ModelItem column) { if (column.IsItemOfType(PlatformTypes.DataGridTextColumn.TypeId)) { return Properties.Resources.Text_Column; } if (column.IsItemOfType(PlatformTypes.DataGridCheckBoxColumn.TypeId)) { return Properties.Resources.CheckBox_Column; } if (column.IsItemOfType(PlatformTypes.DataGridTemplateColumn.TypeId)) { return Properties.Resources.Template_Column; } return string.Empty; }
/// <summary> /// Determine how the control is parented into the TabItem. /// If the control is a TabItem then parent it to the TabControl rather /// than the TabItem (think select, copy, paste a TabItem). /// If the TabItem has content and the content can accept children /// then parent into that and so on. /// </summary> /// <param name="parent">The parent item.</param> /// <param name="childType">The type of child item.</param> /// <returns>Redirected parent.</returns> public override ModelItem RedirectParent(ModelItem parent, Type childType) { _canParent = true; if (parent == null) { throw new ArgumentNullException("parent"); } if (childType == null) { throw new ArgumentNullException("childType"); } // if the tabItem is not activated and/or not within TabControl, // redirect to its parent by returning ourselves. if (parent.IsItemOfType(MyPlatformTypes.TabItem.TypeId) && parent.Parent != null && (!parent.Parent.IsItemOfType(MyPlatformTypes.TabControl.TypeId) || !TabItemDesignModeValueProvider.GetDesignTimeIsSelected(parent))) { _canParent = false; return parent; } // if element being parented is tabItem then add it as sibling of // existing tabItems inside TabControl - redirect to parent; // else parent this control in TabItem's content, if its Panel. if (ModelFactory.ResolveType(parent.Context, MyPlatformTypes.TabItem.TypeId).IsAssignableFrom(childType)) { _canParent = false; return parent; } if (parent.Content != null && parent.Content.IsSet) { // if TabItem has a content and if the content is a panel or a contentcontrol, // let the content act as the parent. ModelItem content = parent.Content.Value; if (content != null) { ViewItem contentView = content.View; if (content.View != null) { // if the content is not visible but the tabItem is selected, // Update this TabItems Layout. if (!contentView.IsVisible && TabItemDesignModeValueProvider.GetDesignTimeIsSelected(parent)) { parent.View.UpdateLayout(); } if (contentView.IsVisible && (content.IsItemOfType(MyPlatformTypes.Panel.TypeId) || content.IsItemOfType(MyPlatformTypes.ContentControl.TypeId))) { return content; } } else { // TabItem has a content already but the content cannot accept parenting right now // so let tabItem's parent take care of parenting. _canParent = false; return parent; } } } // TabItem doesnt have any content and now tabItem itself acts as the parent. return base.RedirectParent(parent, childType); }
/// <summary> /// Determines whether an item may be dragged inside this panel. /// </summary> /// <param name="parent">The panel.</param> /// <param name="childType">The child.</param> /// <returns>True if the item may be parented.</returns> public override bool CanParent(ModelItem parent, Type childType) { if (parent == null) { throw new ArgumentNullException("parent"); } if (childType == null) { throw new ArgumentNullException("childType"); } return parent.IsItemOfType(_targetPanelType); }
/// <summary> /// Parent the given control into the TabControl. /// </summary> /// <param name="parent">The new parent item for child.</param> /// <param name="child">The child item.</param> public override void Parent(ModelItem parent, ModelItem child) { if (parent == null) { throw new ArgumentNullException("parent"); } if (child == null) { throw new ArgumentNullException("child"); } // Clear existing property values that we don't want to apply to the new container. child.Properties[MyPlatformTypes.FrameworkElement.MarginProperty].ClearValue(); child.Properties[MyPlatformTypes.FrameworkElement.HorizontalAlignmentProperty].ClearValue(); child.Properties[MyPlatformTypes.FrameworkElement.VerticalAlignmentProperty].ClearValue(); bool childIsTabItem = child.IsItemOfType(MyPlatformTypes.TabItem.TypeId); // We always accept parenting in TabControl if there is not active focused Task. // If the control being pasted is not TabItem and TabControl is empty (doesnt have any TabItem(s) in it) // then we inject a TabItem with Grid in TabControl and paste the control under consideration in TabItem. // We inject a TabItem also in cases when active TabItem is not capable of parenting concerned control if (!childIsTabItem) { // Based on evaluation done in RedirectParent(), // if any control other than TabItem is being parented to Control in this Parent() call // then we need to add a new Tabitem with Grid in it try { ModelItem newTabItem = ModelFactory.CreateItem(parent.Context, MyPlatformTypes.TabItem.TypeId, CreateOptions.InitializeDefaults); parent.Content.Collection.Add(newTabItem); // activate the newly added tabItem TabItemDesignModeValueProvider.SetDesignTimeIsSelected(newTabItem, true); int index = parent.Content.Collection.Count - 1; // Find a suitable parent for control to be pasted. // Since we always accept parenting for TabControl when there is no active focused task, // we better make sure this works. AdapterService adapterService = parent.Context.Services.GetService<AdapterService>(); if (adapterService != null) { ModelItem targetParent = FindSuitableParent(adapterService, parent, child.ItemType, index); if (targetParent != null) { ParentAdapter parentAdapter = adapterService.GetAdapter<ParentAdapter>(targetParent.ItemType); parentAdapter.Parent(targetParent, child); } else { Debug.Assert(targetParent != null, "Parenting failed!"); } } } catch (Exception ex) { throw new InvalidOperationException("Parenting Failed", ex.InnerException); } } else { // child being added is a TabItem; child.Properties[MyPlatformTypes.TabItem.IsSelectedProperty].ClearValue(); parent.Content.Collection.Add(child); TabItemDesignModeValueProvider.SetDesignTimeIsSelected(child, true); // Activate the newly added tabItem } }
/// <summary> /// Parent the given control into the TabControl. /// </summary> /// <param name="parent">The new parent item for child.</param> /// <param name="child">The child item.</param> public override void Parent(ModelItem parent, ModelItem child) { if (parent == null) { throw new ArgumentNullException("parent"); } if (child == null) { throw new ArgumentNullException("child"); } // Clear existing property values that we don't want to apply to the new container. child.Properties[MyPlatformTypes.FrameworkElement.MarginProperty].ClearValue(); child.Properties[MyPlatformTypes.FrameworkElement.HorizontalAlignmentProperty].ClearValue(); child.Properties[MyPlatformTypes.FrameworkElement.VerticalAlignmentProperty].ClearValue(); bool childIsTabItem = child.IsItemOfType(MyPlatformTypes.TabItem.TypeId); // We always accept parenting in TabControl if there is not active focused Task. // If the control being pasted is not TabItem and TabControl is empty (doesnt have any TabItem(s) in it) // then we inject a TabItem with Grid in TabControl and paste the control under consideration in TabItem. // We inject a TabItem also in cases when active TabItem is not capable of parenting concerned control if (!childIsTabItem) { // Based on evaluation done in RedirectParent(), // if any control other than TabItem is being parented to Control in this Parent() call // then we need to add a new Tabitem with Grid in it try { ModelItem newTabItem = ModelFactory.CreateItem(parent.Context, MyPlatformTypes.TabItem.TypeId, CreateOptions.InitializeDefaults); parent.Content.Collection.Add(newTabItem); // activate the newly added tabItem TabItemDesignModeValueProvider.SetDesignTimeIsSelected(newTabItem, true); int index = parent.Content.Collection.Count - 1; // Find a suitable parent for control to be pasted. // Since we always accept parenting for TabControl when there is no active focused task, // we better make sure this works. AdapterService adapterService = parent.Context.Services.GetService <AdapterService>(); if (adapterService != null) { ModelItem targetParent = FindSuitableParent(adapterService, parent, child.ItemType, index); if (targetParent != null) { ParentAdapter parentAdapter = adapterService.GetAdapter <ParentAdapter>(targetParent.ItemType); parentAdapter.Parent(targetParent, child); } else { Debug.Assert(targetParent != null, "Parenting failed!"); } } } catch (Exception ex) { throw new InvalidOperationException("Parenting Failed", ex.InnerException); } } else { // child being added is a TabItem; child.Properties[MyPlatformTypes.TabItem.IsSelectedProperty].ClearValue(); parent.Content.Collection.Add(child); TabItemDesignModeValueProvider.SetDesignTimeIsSelected(child, true); // Activate the newly added tabItem } }
/// <summary> /// Determine how the control is parented into the TabItem. /// If the control is a TabItem then parent it to the TabControl rather /// than the TabItem (think select, copy, paste a TabItem). /// If the TabItem has content and the content can accept children /// then parent into that and so on. /// </summary> /// <param name="parent">The parent item.</param> /// <param name="childType">The type of child item.</param> /// <returns>Redirected parent.</returns> public override ModelItem RedirectParent(ModelItem parent, Type childType) { _canParent = true; if (parent == null) { throw new ArgumentNullException("parent"); } if (childType == null) { throw new ArgumentNullException("childType"); } // if the tabItem is not activated and/or not within TabControl, // redirect to its parent by returning ourselves. if (parent.IsItemOfType(MyPlatformTypes.TabItem.TypeId) && parent.Parent != null && (!parent.Parent.IsItemOfType(MyPlatformTypes.TabControl.TypeId) || !TabItemDesignModeValueProvider.GetDesignTimeIsSelected(parent))) { _canParent = false; return(parent); } // if element being parented is tabItem then add it as sibling of // existing tabItems inside TabControl - redirect to parent; // else parent this control in TabItem's content, if its Panel. if (ModelFactory.ResolveType(parent.Context, MyPlatformTypes.TabItem.TypeId).IsAssignableFrom(childType)) { _canParent = false; return(parent); } if (parent.Content != null && parent.Content.IsSet) { // if TabItem has a content and if the content is a panel or a contentcontrol, // let the content act as the parent. ModelItem content = parent.Content.Value; if (content != null) { ViewItem contentView = content.View; if (content.View != null) { // if the content is not visible but the tabItem is selected, // Update this TabItems Layout. if (!contentView.IsVisible && TabItemDesignModeValueProvider.GetDesignTimeIsSelected(parent)) { parent.View.UpdateLayout(); } if (contentView.IsVisible && (content.IsItemOfType(MyPlatformTypes.Panel.TypeId) || content.IsItemOfType(MyPlatformTypes.ContentControl.TypeId))) { return(content); } } else { // TabItem has a content already but the content cannot accept parenting right now // so let tabItem's parent take care of parenting. _canParent = false; return(parent); } } } // TabItem doesnt have any content and now tabItem itself acts as the parent. return(base.RedirectParent(parent, childType)); }