/// <summary> /// Create item container based on ItemTemplate or ItemTemplateSelector /// </summary> private View CreateItemView(object model) { View item = null; if (ItemTemplateSelector != null) { DataTemplate containerTemplate = ItemTemplateSelector.SelectTemplate(model, null) as DataTemplate; item = containerTemplate.CreateContent() as View; } else if (ItemTemplate != null) { item = ItemTemplate.CreateContent() as View; } else { item = new ContentView(); } if (item == null) { throw new Exception("ItemTemplate is not subclass of ContentView"); } item.BindingContext = model; return(item); }
private ItemInfo AddItem(object item) { var trans = transform; var itemTemplate = ItemTemplateSelector != null?ItemTemplateSelector.SelectTemplate(item) : _itemTemplate; var control = Instantiate(itemTemplate); var rect = control.GetComponent <RectTransform>(); if (rect == null) { control.transform.parent = trans; } else { rect.SetParent(trans, false); } var info = new ItemInfo(item, control, rect); _items.Add(info); control.SetActive(true); var context = control.GetComponent <DataContext>(); if (context != null) { context.UpdateValue(item); } OnItemAdded(info); return(info); }
protected virtual HubSection GenerateHubSection(object item) { var section = new HubSection() { DataContext = item, Header = item, ContentTemplate = ItemTemplateSelector == null ? ItemTemplate : ItemTemplateSelector.SelectTemplate(item), }; if (!String.IsNullOrEmpty(IsHeaderInteractivePath)) { section.SetBinding(HubSection.IsHeaderInteractiveProperty, new Binding() { Path = new PropertyPath(this.IsHeaderInteractivePath) }); } if (!String.IsNullOrEmpty(HeaderPath)) { section.SetBinding(HubSection.HeaderProperty, new Binding() { Path = new PropertyPath(this.HeaderPath) }); } return(section); }
private void UpdatePrimaryCommands() { if (AssociatedObject == null) { return; } if (PrimaryCommands == null) { return; } AssociatedObject.PrimaryCommands.Clear(); if (!(PrimaryCommands is IEnumerable enumerable)) { AssociatedObject.PrimaryCommands.Clear(); return; } foreach (var command in enumerable) { var template = ItemTemplateSelector.SelectTemplate(command, AssociatedObject); if (!(template?.LoadContent() is FrameworkElement dependencyObject)) { continue; } dependencyObject.DataContext = command; if (dependencyObject is ICommandBarElement icommandBarElement) { AssociatedObject.PrimaryCommands.Add(icommandBarElement); } } }
private ViewCell CreateViewCell(object item) { var command = SelectedCommand ?? new Command((obj) => { var args = new ItemTappedEventArgs(ItemsSource, item); ItemSelected?.Invoke(this, args); }); var commandParameter = SelectedCommandParameter ?? item; var itemTemplateInstance = ItemTemplateSelector != null?ItemTemplateSelector.SelectTemplate(item, null) : ItemTemplate; var viewCell = itemTemplateInstance.CreateContent() as ViewCell; var tap = new TapGestureRecognizer(); tap.Command = command; tap.CommandParameter = commandParameter; tap.NumberOfTapsRequired = 1; viewCell.View.BindingContext = item; viewCell.View.GestureRecognizers.Add(tap); tap.Tapped += Tap_Tapped; return(viewCell); }
/// <summary> /// Show notification using predefined DataTemplate from ItemTemplateSelector /// </summary> /// <param name="notificationType">Type of the notification</param> /// <param name="duration">Displayed duration of the notification in ms (less or equal 0 means infinite duration)</param> public void Show(InAppNotificationType notificationType, int duration = 0) { var template = ItemTemplateSelector.SelectTemplate(notificationType) ?? null; ContentTemplate = template; Content = null; //TODO set datacontext if notification type would be recognized by appropriate ViewModel Show(duration); }
/// <summary> /// Adds an item container to this control for the specified item. /// </summary> /// <param name="index">The index of the item being added.</param> /// <param name="item">The item for which to add an item container.</param> /// <returns>The item container that was added.</returns> private DependencyObject AddItemContainer(Int32 index, Object item) { if (ItemsPanelElement == null) { return(null); } var container = default(DependencyObject); if (IsItemContainer(item)) { container = (DependencyObject)item; } else { container = GetContainerForItemOverride(); var template = ItemTemplateSelector?.SelectTemplate(item, container) ?? ItemTemplate; if (template != null) { var templateInstance = template.LoadContent(item, item?.GetType()); PrepareContainerForItemOverride(container, templateInstance); } else { PrepareContainerForItemOverride(container, item); } } itemContainerGenerator.AssociateContainerWithItem(container, item); itemContainers.Insert(index, container); var containerElement = container as UIElement; if (containerElement != null) { ItemsPanelElement.Children.Insert(index, containerElement); containerElement.ChangeLogicalParent(this); } SetValue(HasItemsPropertyKey, true); return(container); }
void CreatePin(object newItem) { DataTemplate itemTemplate = ItemTemplate; if (itemTemplate == null) { itemTemplate = ItemTemplateSelector?.SelectTemplate(newItem, this); } if (itemTemplate == null) { return; } var pin = (Pin)itemTemplate.CreateContent(); pin.BindingContext = newItem; _pins.Add(pin); }
private MapMarker CreateMarker(object sourceItem) { var template = ItemTemplateSelector != null ? ItemTemplateSelector.SelectTemplate(sourceItem, null) ?? ItemTemplate : ItemTemplate; if (template == null) { return(null); } var dataElement = template.LoadContent() as FrameworkElement; if (dataElement == null) { return(null); } var labelTemplate = _mapControl.ShowLabel ? (LabelTemplateSelector != null ? LabelTemplateSelector.SelectTemplate(sourceItem, null) ?? LabelTemplate : LabelTemplate) : null; var container = new MapMarkerContainer(); container.SetValue(StyleProperty, MarkerContainerStyle); container.Content = dataElement; container.DataContext = sourceItem; if (labelTemplate != null) { container.LabelContent = labelTemplate.LoadContent() as FrameworkElement; } var marker = new MapMarker(container); marker.MouseDown += (sender, args) => SelectMarker((MapMarker)sender, Keyboard.IsKeyDown(Key.LeftShift) || (Keyboard.IsKeyDown(Key.RightShift) || Keyboard.IsKeyDown(Key.LeftCtrl) || (Keyboard.IsKeyDown(Key.RightCtrl)))); return(marker); }
private Visual3D CreateVisualFromModel(object item) { if (this.ItemTemplate != null) { return(this.ItemTemplate.CreateItem(item)); } else if (ItemTemplateSelector != null) { var viewPort = Visual3DHelper.GetViewport3D(this); var dataTemplate = ItemTemplateSelector.SelectTemplate(item, viewPort); if (dataTemplate != null) { return(dataTemplate.CreateItem(item)); } else { return(item as Visual3D); } } else { return(item as Visual3D); } }
protected virtual void ItemsModel3D_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Remove: if (e.OldItems != null) { foreach (var item in e.OldItems) { if (elementDict.TryGetValue(item, out Element3D element)) { Children.Remove(element); elementDict.Remove(item); element.Dispose(); } } InvalidateRender(); } break; case NotifyCollectionChangedAction.Reset: Clear(); break; } switch (e.Action) { case NotifyCollectionChangedAction.Reset: if (this.ItemsSource != null) { if (this.ItemTemplate == null) { if (ItemTemplateSelector != null) { foreach (var item in this.ItemsSource) { var dataTemplate = ItemTemplateSelector.SelectTemplate(item, null); var model = dataTemplate.LoadContent() as Element3D; if (model != null) { model.DataContext = item; this.Children.Add(model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } else { foreach (var item in this.ItemsSource) { var model = item as Element3D; if (model != null) { this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } } else { foreach (var item in this.ItemsSource) { var model = this.ItemTemplate.LoadContent() as Element3D; if (model != null) { model.DataContext = item; this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } } InvalidateRender(); break; case NotifyCollectionChangedAction.Add: case NotifyCollectionChangedAction.Replace: if (e.NewItems != null) { if (this.ItemTemplate != null) { foreach (var item in e.NewItems) { var model = this.ItemTemplate.LoadContent() as Element3D; if (model != null) { model.DataContext = item; this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } else { if (ItemTemplateSelector != null) { foreach (var item in e.NewItems) { var dataTemplate = ItemTemplateSelector.SelectTemplate(item, null); var model = dataTemplate.LoadContent() as Element3D; if (model != null) { model.DataContext = item; this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } else { foreach (var item in e.NewItems) { var model = item as Element3D; if (model != null) { this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } } } break; } }
/// <summary> /// Handles changes in the ItemsSource property. /// </summary> /// <param name="e"> /// The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data. /// </param> /// <exception cref="System.InvalidOperationException"> /// Cannot create a Model3D from ItemTemplate. /// </exception> protected virtual void ItemsSourceChanged(DependencyPropertyChangedEventArgs e) { if (e.OldValue is INotifyCollectionChanged o) { o.CollectionChanged -= ItemsModel3D_CollectionChanged; } foreach (Element3D item in Children) { item.DataContext = null; } Clear(); if (e.NewValue is INotifyCollectionChanged n) { n.CollectionChanged -= ItemsModel3D_CollectionChanged; n.CollectionChanged += ItemsModel3D_CollectionChanged; } if (ItemsSource == null) { return; } if (this.ItemTemplate == null) { if (ItemTemplateSelector != null) { foreach (var item in this.ItemsSource) { var dataTemplate = ItemTemplateSelector.SelectTemplate(item, null); var model = dataTemplate.LoadContent() as Element3D; if (model != null) { model.DataContext = item; this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } else { foreach (var item in this.ItemsSource) { var model = item as Element3D; if (model != null) { this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } } else { foreach (var item in this.ItemsSource) { var model = this.ItemTemplate.LoadContent() as Element3D; if (model != null) { model.DataContext = item; this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } if (Children.Count > 0) { var groupNode = SceneNode as GroupNode; groupNode.OctreeManager?.RequestRebuild(); } }
protected virtual void PrepareContainerForItemOverride(object item, FrameworkElement container) { if (container is IItemContainer) { DataTemplate itemTemplate = ItemTemplate ?? (ItemTemplateSelector != null ? ItemTemplateSelector.SelectTemplate(item, container) : null); Style itemContainerStyle = ItemContainerStyle ?? (ItemContainerStyleSelector != null ? ItemContainerStyleSelector.SelectStyle(item, container) : null); ((IItemContainer)container).PrepareContainerForItem(item, itemTemplate, itemContainerStyle); } }
/// <summary> /// Generates content based on the template information /// </summary> /// <exception cref="System.Exception">ItemsPanel must be a block element</exception> private void GenerateContent() { if (!IsLoaded) { return; } Blocks.Clear(); if ((ItemTemplate == null && ItemTemplateSelector == null) || ItemsSource == null) { return; } FrameworkContentElement panel = null; foreach (var data in ItemsSource) { if (panel == null) { if (ItemsPanel == null) { panel = this; } else { var itemsPanelTemplate = LoadDataTemplate(ItemsPanel); foreach (var templatePart in itemsPanelTemplate) { var block = templatePart as Block; if (block == null) { throw new Exception("ItemsPanel must be a block element"); } Blocks.Add(block); panel = DocEx.GetItemsHost(block); if (panel == null) { throw new Exception("ItemsHost not found. Did you forget to specify DocEx.IsItemsHost?"); } } } } var itemTemplate = ItemTemplateSelector != null?ItemTemplateSelector.SelectTemplate(data, this) : ItemTemplate; var elements = LoadDataTemplate(itemTemplate); foreach (var element in elements) { element.DataContext = data; var expressions = element.GetValue(DocEx.ForceRefreshExpressionProperty); if (expressions != null) { var bindings = expressions as List <BindingToSet>; if (bindings != null) { foreach (var binding in bindings) { if (binding.Binding.Source == null && binding.Binding.RelativeSource == null) { binding.Binding.Source = data; } var frameworkElement = binding.DependencyObject as FrameworkElement; if (frameworkElement != null) { frameworkElement.SetBinding(binding.Property, binding.Binding); } else { var frameworkContentElement = binding.DependencyObject as FrameworkContentElement; if (frameworkContentElement != null) { frameworkContentElement.SetBinding(binding.Property, binding.Binding); } } } } } var section = panel as Section; var paragraph = panel as Paragraph; var inline = element as Inline; var tableRowGroup = panel as TableRowGroup; var tableRow = element as TableRow; var list = panel as List; var listItem = element as ListItem; if (section != null) { section.Blocks.Add(ConvertToBlock(data, element)); } else if (paragraph != null && inline != null) { if (inline.Parent == null) { paragraph.Inlines.Add(inline); } } else if (tableRowGroup != null && tableRow != null) { if (tableRow.Parent == null) { tableRowGroup.Rows.Add(tableRow); } } else if (list != null && listItem != null) { if (listItem.Parent == null) { list.ListItems.Add(listItem); } } else if (panel != null) { throw new Exception(String.Format("Can't add an instance of {0} to an instance of {1}", element.GetType(), panel.GetType())); } else { throw new Exception("Unable to add child elements."); } } } }
protected virtual void ItemsModel3D_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Remove: if (e.OldItems != null) { if (_tryExperiment && IsPanelHookerView && IsUnhookingPanel(e)) { var key = e.OldItems[0]; if (elementDict.TryGetValue(key, out Element3D element)) { Children.Remove(element); elementDict.Remove(key); _transferElements.Add(key, element); } } else { foreach (var item in e.OldItems) { Element3D element; if (elementDict.TryGetValue(item, out element)) { Children.Remove(element); elementDict.Remove(item); } } } //foreach (var item in e.OldItems) //{ // Element3D element; // if (elementDict.TryGetValue(item, out element)) // { // Children.Remove(element); // elementDict.Remove(item); // } //} InvalidateRender(); } break; case NotifyCollectionChangedAction.Reset: Clear(); break; } switch (e.Action) { case NotifyCollectionChangedAction.Reset: if (this.ItemsSource != null) { if (this.ItemTemplate == null) { if (ItemTemplateSelector != null) { foreach (var item in this.ItemsSource) { var dataTemplate = ItemTemplateSelector.SelectTemplate(item, null); var model = dataTemplate.LoadContent() as Element3D; if (model != null) { model.DataContext = item; this.Children.Add(model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } else { foreach (var item in this.ItemsSource) { var model = item as Element3D; if (model != null) { this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } } else { foreach (var item in this.ItemsSource) { var model = this.ItemTemplate.LoadContent() as Element3D; if (model != null) { model.DataContext = item; this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } } InvalidateRender(); break; case NotifyCollectionChangedAction.Add: case NotifyCollectionChangedAction.Replace: if (e.NewItems != null) { if (this.ItemTemplate != null) { //foreach (var item in e.NewItems) //{ // var model = this.ItemTemplate.LoadContent() as Element3D; // if (model != null) // { // model.DataContext = item; // this.Children.Add(model); // elementDict.Add(item, model); // } // else // { // throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); // } //} if (_tryExperiment && IsPanelHookerView && IsHookingPanel(e)) { var key = e.NewItems[0]; if (_transferElements.TryGetValue(key, out Element3D element)) { Children.Add(element); elementDict.Add(key, element); _transferElements.Remove(key); // provo a rinfrescare la traslazione var me = key as MachineElements.ViewModels.Interfaces.IMachineElementViewModel; element.Transform = me.Transform; } } else { foreach (var item in e.NewItems) { var model = this.ItemTemplate.LoadContent() as Element3D; if (model != null) { model.DataContext = item; this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } } else { if (ItemTemplateSelector != null) { foreach (var item in e.NewItems) { var dataTemplate = ItemTemplateSelector.SelectTemplate(item, null); var model = dataTemplate.LoadContent() as Element3D; if (model != null) { model.DataContext = item; this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } else { foreach (var item in e.NewItems) { var model = item as Element3D; if (model != null) { this.Children.Add(model); elementDict.Add(item, model); } else { throw new InvalidOperationException("Cannot create a Model3D from ItemTemplate."); } } } } } break; } }