public static DataTemplate ResolveTemplate( DataTemplate dataTemplate, DataTemplateSelector dataTemplateSelector, object data, DependencyObject container) { var template = dataTemplate; if (template != null) { return(template); } if (dataTemplateSelector != null) { var result = dataTemplateSelector.SelectTemplate(data); if (result == null && container != null && !FeatureConfiguration.DataTemplateSelector.UseLegacyTemplateSelectorOverload) { result = dataTemplateSelector.SelectTemplate(data, container); } return(result); } return(null); }
void ItemsSourceChanged() { _stack.Children.Clear(); foreach (var item in ItemsSource) { if (ItemTemplate is DataTemplateSelector) { DataTemplateSelector selector = (DataTemplateSelector)ItemTemplate; DataTemplate template = selector.SelectTemplate(item, null); var view = (View)template.CreateContent(); var bindableObject = view as BindableObject; if (bindableObject != null) { bindableObject.BindingContext = item; } _stack.Children.Add(view); } else if (ItemTemplate is DataTemplate) { var view = (View)ItemTemplate.CreateContent(); var bindableObject = view as BindableObject; if (bindableObject != null) { bindableObject.BindingContext = item; } _stack.Children.Add(view); } } if (_selectedIndex >= 0) { SelectedIndex = _selectedIndex; } }
protected override Cell CreateDefault(object item) { if (currentItemSelector != null) { var template = currentItemSelector.SelectTemplate(item, this); if (template != null) { var templateInstance = template.CreateContent(); // see if it's a view or a cell var templateView = templateInstance as View; var templateCell = templateInstance as Cell; if (templateView == null && templateCell == null) { throw new InvalidOperationException("DataTemplate must be either a Cell or a View"); } if (templateView != null) // we got a view, wrap in a cell { templateCell = new ViewCell { View = templateView, Height = templateView.HeightRequest } } ; return(templateCell); } } return(base.CreateDefault(item)); }
public override void OnApplyTemplate() { base.OnApplyTemplate(); buttonExpand = (ToggleButton)GetTemplateChild("buttonExpand"); button = (Button)GetTemplateChild("button"); menu = (ContextMenu)GetTemplateChild("menu"); if (menu != null) { menu.DataContext = DataContext; menu.Opened += menu_Opened; BreadcrumbItem templatedParent = TemplatedParent as BreadcrumbItem; if (templatedParent != null && templatedParent.Items.Count > 0) { DataTemplateSelector selector = VisualTreHelperEx.GetDefaultSelector(); DataTemplate template = selector.SelectTemplate(templatedParent.Items[0], this); menu.ItemTemplate = template; } menu.CommandBindings.Add(new CommandBinding(InvokeMenuCommand, CommandBindingToMenuItem_Executed)); } DependencyPropertyDescriptor isPressedProperty = DependencyPropertyDescriptor.FromProperty(IsPressedProperty, typeof(Button)); isPressedProperty.AddValueChanged(button, OnIsPressedChangedIntern); }
/// <summary> /// Generates an container element for the specified input data object. /// </summary> /// <param name="item">The data object for which to generate container.</param> /// <param name="template">The <see cref="DataTemplate"/> that specifies the visualization of the data object.</param> /// <param name="templateSelector">The <see cref="DataTemplateSelector"/> object that returns a <see cref="DataTemplate"/>.</param> /// <param name="style">The <see cref="Style"/> that is applied to the generated container element.</param> /// <param name="styleSelector">The <see cref="StyleSelector"/> object that returns a <see cref="Style"/>.</param> /// <returns>The container element for the <paramref name="item"/>.</returns> private DependencyObject GetContainer(object item, DataTemplate template, DataTemplateSelector templateSelector, Style style, StyleSelector styleSelector) { DependencyObject container = null; if (template == null && templateSelector != null) { template = templateSelector.SelectTemplate(item, null); } if (template != null) { container = template.LoadContent(); } if (container == null) { container = CreateContainer(item); if (container == null) { throw Error.ReturnsNull(nameof(CreateContainer)); } } container.SetValue(ItemProperty, item); container.SetValue(FrameworkElement.DataContextProperty, item); ApplyStyle(container, item, style, styleSelector); OnContainerCreated(container, item); return(container); }
public void When_Base_Container_Is_Null() { var sut = new DataTemplateSelector(); var act = () => sut.SelectTemplate(0, null); act.Should().Throw <ArgumentException>(); }
public void ApplyBinding() { object dataContext = base.DataContext; if (dataContext != null) { DataTemplate dataTemplate = this.HeaderTemplate; DataTemplateSelector headerTemplateSelector = this.HeaderTemplateSelector; if (headerTemplateSelector != null) { dataTemplate = headerTemplateSelector.SelectTemplate(dataContext, this); } if (dataTemplate == null) { DataTemplateKey resourceKey = BreadcrumbItem.GetResourceKey(dataContext); if (resourceKey != null) { dataTemplate = (base.TryFindResource(resourceKey) as DataTemplate); } } this.SelectedItem = null; HierarchicalDataTemplate hierarchicalDataTemplate = dataTemplate as HierarchicalDataTemplate; if (dataTemplate != null) { this.Header = dataTemplate.LoadContent(); } else { this.Header = dataContext; } this.DataContext = dataContext; if (hierarchicalDataTemplate != null) { this.SetBinding(ItemsControl.ItemsSourceProperty, hierarchicalDataTemplate.ItemsSource); } BreadcrumbBar breadcrumbBar = this.BreadcrumbBar; if (breadcrumbBar != null) { if (this.TraceBinding == null) { this.TraceBinding = breadcrumbBar.TraceBinding; } if (this.ImageBinding == null) { this.ImageBinding = breadcrumbBar.ImageBinding; } } if (this.TraceBinding != null) { this.SetBinding(BreadcrumbItem.TraceProperty, this.TraceBinding); } if (this.ImageBinding != null) { this.SetBinding(BreadcrumbItem.ImageProperty, this.ImageBinding); } this.ApplyProperties(dataContext); } }
protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { base.PrepareContainerForItemOverride(element, item); DataTemplateSelector selector = this.ItemTemplateSelector; if (selector != null) { ((ContentPresenter)element).ContentTemplate = selector.SelectTemplate(item, element); } }
private FrameworkElement CreateCellControl(object dataItem, DataTemplateSelector templateSelector, DataGridCell cell) { var content = CellContentSelector?.SelectContent(dataItem, this, cell); var ctrl = new ContentPresenter() { Content = content }; ctrl.ContentTemplate = templateSelector.SelectTemplate(content, ctrl); return(ctrl); }
/// <summary> /// Invoked when the value of the Content property changes. /// </summary> /// <param name="oldContent">The old value of the Content property.</param> /// <param name="newContent">The new value of the Content property.</param> protected override void OnContentChanged(object oldContent, object newContent) { // There is a bug in the standard content control that trashes the value passed into the SelectTemplateCore method. This is a // work-around that allows the same basic structure and can hopefully be replaced when the bug is fixed. Basically take the new content // and figure out what template should be used with it based on the structure of the template selector. DataTemplateSelector dataTemplateSelector = this.ContentTemplateSelector as DataTemplateSelector; if (dataTemplateSelector != null) { this.ContentTemplate = dataTemplateSelector.SelectTemplate(newContent, null); } // Allow the base class to handle the rest of the call. base.OnContentChanged(oldContent, newContent); }
private void UpdateItemTemplate(LoopingListDataItem dataItem, LoopingListItem visualItem) { DataTemplate template = null; DataTemplateSelector selector = this.itemTemplateSelectorCache; if (selector != null) { template = selector.SelectTemplate(dataItem, visualItem); } if (template == null) { template = this.itemTemplateCache; } visualItem.ContentTemplate = template; }
public static View ViewFor(this BindableObject This, object item, DataTemplateSelector selector) { if (selector != null) { var template = selector.SelectTemplate(item, This); if (template != null) { var templateInstance = template.CreateContent(); // see if it's a view or a cell var templateView = templateInstance as View; if (templateView == null) throw new InvalidOperationException("DataTemplate must be a View"); return templateView; } } return null; }
/// <summary> /// Returns the template view that matches the conditions of the data template <paramref name="selector"/> for the specified <paramref name="item"/> /// </summary> /// <param name="container">The bindable object containing the template definition for the specified <paramref name="item"/>.</param> /// <param name="item">The object for which to return the corresponding template view.</param> /// <param name="selector">The <see cref="DataTemplateSelector"/> that contains the custom logic to determine which view to return for the specified <paramref name="item"/>.</param> /// <returns>Returns a <see cref="View"/> instance.</returns> /// <exception cref="InvalidOperationException">Thrown when data template associated with <paramref name="item"/> is not a <see cref="View"/>.</exception> public static View ViewFor(this BindableObject container, object item, DataTemplateSelector selector) { if (selector != null) { var template = selector.SelectTemplate(item, container); if (template != null) { var content = template.CreateContent(); var view = content as View; if (view == null) { throw new InvalidOperationException("DataTemplate must be a View."); } return(view); } } return(null); }
private HashSet <DataTemplate> RegisterCellDataTemplates(DataTemplateSelector dataTemplateSelector) { HashSet <DataTemplate> dataTemplates = new HashSet <DataTemplate>(); foreach (var item in _itemsSource) { var dataTemplate = dataTemplateSelector.SelectTemplate(item, Element); int itemViewType = dataTemplates.IndexOf(dataTemplate); if (itemViewType == -1) { itemViewType = dataTemplates.Count; dataTemplates.Add(dataTemplate); } } for (int i = 0; i < dataTemplates.Count; i++) { Control.RegisterClassForCell(typeof(iOSViewCell), IdentifierFormatter.FormatDataTemplateCellIdentifier(i)); } return(dataTemplates); }
/// <summary> /// Cells for. /// </summary> /// <param name="This">The this.</param> /// <param name="item">The item.</param> /// <param name="selector">The selector.</param> /// <returns>Cell.</returns> /// <exception cref="System.InvalidOperationException">DataTemplate must be either a Cell or a View</exception> public static Cell CellFor(this BindableObject This, object item, DataTemplateSelector selector) { if (selector != null) { var template = selector.SelectTemplate(item, This); if (template != null) { var templateInstance = template.CreateContent(); // see if it's a view or a cell var templateView = templateInstance as View; var templateCell = templateInstance as Cell; if (templateView == null && templateCell == null) throw new InvalidOperationException("DataTemplate must be either a Cell or a View"); if (templateView != null) // we got a view, wrap in a cell templateCell = new ViewCell { View = templateView }; return templateCell; } } return null; }
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container) { if (item is IGroupHeader) { DataTemplateSelector headerSelector = _groupGridView.GroupHeaderTemplateSelector; if (headerSelector != null) { return(headerSelector.SelectTemplate(item, container)); } return(_groupGridView.GroupHeaderTemplate); } else if (item is IGroupFooter) { DataTemplateSelector footerTemplateSelector = _groupGridView.GroupFooterTemplateSelector; if (footerTemplateSelector != null) { return(footerTemplateSelector.SelectTemplate(item, container)); } return(_groupGridView.GroupFooterTemplate); } else { DataTemplateSelector itemTemplateSelector = _groupGridView.ItemTemplateSelector; if (itemTemplateSelector != null) { return(itemTemplateSelector.SelectTemplate(item, container)); } return(_groupGridView.ItemTemplate); } }
/// <summary> /// Prepare a PrepareHeaderedItemsControlContainer container for an /// item. /// </summary> /// <param name="control">Container to prepare.</param> /// <param name="item">Item to be placed in the container.</param> /// <param name="parentItemsControl">The parent ItemsControl.</param> /// <param name="parentItemContainerStyle"> /// The ItemContainerStyle for the parent ItemsControl. /// </param> private static void PrepareHeaderedItemsControlContainer(HeaderedItemsControl control, object item, ItemsControl parentItemsControl, Style parentItemContainerStyle) { try { if (control != item) { // Copy the ItemsControl properties from parent to child DataTemplate parentItemTemplate = parentItemsControl.ItemTemplate; DataTemplateSelector parentItemTemplateSelector = parentItemsControl.ItemTemplateSelector; if (parentItemTemplateSelector != null) { control.SetValue(HeaderedItemsControl.ItemTemplateSelectorProperty, parentItemTemplateSelector); parentItemTemplate = parentItemTemplateSelector.SelectTemplate(item, control); } else if (parentItemTemplate != null) { control.SetValue(HeaderedItemsControl.ItemTemplateProperty, parentItemTemplate); } if (parentItemContainerStyle != null && HasDefaultValue(control, HeaderedItemsControl.ItemContainerStyleProperty)) { control.SetValue(HeaderedItemsControl.ItemContainerStyleProperty, parentItemContainerStyle); } // Copy the Header properties from parent to child if (control.HeaderIsItem || HasDefaultValue(control, HeaderedItemsControl.HeaderProperty)) { control.Header = item; control.HeaderIsItem = true; } if (parentItemTemplate != null) { control.SetValue(HeaderedItemsControl.HeaderTemplateProperty, parentItemTemplate); } if (parentItemContainerStyle != null && control.Style == null) { control.SetValue(HeaderedItemsControl.StyleProperty, parentItemContainerStyle); } // Note: this is where we would apply the HeaderTemplateSelector // (if implemented) or attempt to lookup the implicit template // for the type of the item if the headerTemplate were null. // Setup a hierarchical template //HierarchicalDataTemplate headerTemplate = parentItemTemplate as HierarchicalDataTemplate; var headerTemplate = parentItemTemplate as DataTemplate; if (headerTemplate != null) { var hierarchy = DataTemplateExtensions.GetHierarchy(headerTemplate); if (hierarchy != null && hierarchy.ItemsSource != null && HasDefaultValue(control, HeaderedItemsControl.ItemsSourceProperty)) { control.SetBinding( HeaderedItemsControl.ItemsSourceProperty, new Binding { Converter = hierarchy.ItemsSource.Converter, ConverterLanguage = hierarchy.ItemsSource.ConverterLanguage, ConverterParameter = hierarchy.ItemsSource.ConverterParameter, Mode = hierarchy.ItemsSource.Mode, //NotifyOnValidationError = headerTemplate.ItemsSource.NotifyOnValidationError, Path = hierarchy.ItemsSource.Path, Source = control.Header, //ElementName = //ValidatesOnExceptions = headerTemplate.ItemsSource.ValidatesOnExceptions }); } if (hierarchy != null && hierarchy.IsItemTemplateSet && control.ItemTemplate == parentItemTemplate) { control.ClearValue(HeaderedItemsControl.ItemTemplateProperty); if (hierarchy.ItemTemplate != null) { control.ItemTemplate = hierarchy.ItemTemplate; } } if (hierarchy != null && hierarchy.IsItemContainerStyleSet && control.ItemContainerStyle == parentItemContainerStyle) { control.ClearValue(HeaderedItemsControl.ItemContainerStyleProperty); if (hierarchy.ItemContainerStyle != null) { control.ItemContainerStyle = hierarchy.ItemContainerStyle; } } } } } catch (COMException) { Debug.Assert(false, "Due to some change in the platform in Windows 10 - you now need to set the ItemTemplate before setting the ItemsSource when using a TreeView."); } }
public object Convert(object value, Type targetType, object parameter, String a) { DataTemplateSelector Selector = (DataTemplateSelector)parameter; return(Selector.SelectTemplate(value)); }
public static DataTemplate ResolveTemplate(DataTemplate dataTemplate, DataTemplateSelector dataTemplateSelector, Func <object> data) { return(dataTemplate ?? dataTemplateSelector?.SelectTemplate(data?.Invoke())); }
public static DataTemplate SelectTemplate(PropertyGridProperty property, object item, DependencyObject container) { if (property == null) { throw new ArgumentNullException("property"); } PropertyGridOptionsAttribute att = FromProperty(property); if (att == null) { return(null); } if (att.EditorDataTemplateResourceKey != null) { if (Application.Current != null) { DataTemplate dt = (DataTemplate)Application.Current.TryFindResource(att.EditorDataTemplateResourceKey); if (dt != null) { return(dt); } } FrameworkElement fe = container as FrameworkElement; if (fe != null) { DataTemplate dt = (DataTemplate)fe.TryFindResource(att.EditorDataTemplateResourceKey); if (dt != null) { return(dt); } } return(null); } if (att.EditorType != null) { object editor = Activator.CreateInstance(att.EditorType); if (att.EditorDataTemplateSelectorPropertyPath != null) { DataTemplateSelector dts = (DataTemplateSelector)DataBindingEvaluator.GetPropertyValue(editor, att.EditorDataTemplateSelectorPropertyPath); return(dts != null?dts.SelectTemplate(item, container) : null); } if (att.EditorDataTemplatePropertyPath != null) { return((DataTemplate)DataBindingEvaluator.GetPropertyValue(editor, att.EditorDataTemplatePropertyPath)); } ContentControl cc = editor as ContentControl; if (cc != null) { if (cc.ContentTemplateSelector != null) { DataTemplate template = cc.ContentTemplateSelector.SelectTemplate(item, container); if (template != null) { return(template); } } return(cc.ContentTemplate); } ContentPresenter cp = editor as ContentPresenter; if (cp != null) { if (cp.ContentTemplateSelector != null) { DataTemplate template = cp.ContentTemplateSelector.SelectTemplate(item, container); if (template != null) { return(template); } } return(cp.ContentTemplate); } } return(null); }
/// <summary> /// Appies the binding to the breadcrumb item. /// </summary> public void ApplyBinding() { object item = DataContext; if (item == null) { return; } BreadcrumbItem root = this; DataTemplate template = HeaderTemplate; DataTemplateSelector templateSelector = HeaderTemplateSelector; if (templateSelector != null) { template = templateSelector.SelectTemplate(item, root); } if (template == null) { DataTemplateKey key = GetResourceKey(item); if (key != null) { template = TryFindResource(key) as DataTemplate; } } root.SelectedItem = null; HierarchicalDataTemplate hdt = template as HierarchicalDataTemplate; if (template != null) { root.Header = template.LoadContent(); } else { root.Header = item; } root.DataContext = item; if (hdt != null) { // bind the Items to the hierarchical data template: root.SetBinding(BreadcrumbItem.ItemsSourceProperty, hdt.ItemsSource); } BreadcrumbBar bar = BreadcrumbBar; if (bar != null) { if (TraceBinding == null) { TraceBinding = bar.TraceBinding; } if (ImageBinding == null) { ImageBinding = bar.ImageBinding; } } if (TraceBinding != null) { root.SetBinding(BreadcrumbItem.TraceProperty, TraceBinding); } if (ImageBinding != null) { root.SetBinding(BreadcrumbItem.ImageProperty, ImageBinding); } ApplyProperties(item); }
private static Grid GenerateLayoutGrid(BindableLayout bindableLayout, DataTemplateSelector panelTemplateSelector, int LayoutIndex, bool useFixedAspectRatioGrid) { //Resolve Styles ResourceDictionary rdStyleOverrides = null; string xamlStyling = BuildPanelStylingXaml(bindableLayout.CurrentSkin.PanelStylingFlags); if (xamlStyling != null) { rdStyleOverrides = XamlHelper2.ParseXamlResourceDictionary(xamlStyling); } Style stSkinPanelOuterHostStyle = XamlHelper2.ResolveStyleFromResourceDictionary(rdStyleOverrides, "SkinPanelOuterHostStyle"); Style stSkinPanelHostStyle = XamlHelper2.ResolveStyleFromResourceDictionary(rdStyleOverrides, "SkinPanelHostStyle"); DataTemplate dtPanelTemplateHeader = XamlHelper2.ResolveStylingDataTemplateFromResourceDictionary(rdStyleOverrides, "PanelTemplateHeader");; //Style stSkinPanelOuterHostStyle = BuildSkinPanelOuterHostStyle(bindableLayout.CurrentSkin.PanelStylingFlags); Grid grd = null; if (useFixedAspectRatioGrid) { double div = Math.Max((bindableLayout.CurrentSkin.VerticalResolution - bindableLayout.CurrentSkin.TitleHeight), 10); double ratio = (bindableLayout.CurrentSkin.HorizontalResolution / div); ratio = Math.Clamp(ratio, 0.05, 4); grd = new FixedAspectRatioGridControl() { Mode = FixedRatioMode.AdjustWidth, AspectRatio = ratio }; } else { grd = new Grid() { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }; } //Grid.SetRow(grd, 1); try { for (int z = 0; z < bindableLayout.CurrentSkin.LayoutColumns; z++) { grd.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }); } for (int z = 0; z < bindableLayout.CurrentSkin.LayoutRows; z++) { grd.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) }); } foreach (var item in bindableLayout.SkinPanels) { if ((item.IsVisible) && (LayoutIndex == item.LayoutIndex)) { bool hasStylingFlags = false; Style stItemSkinPanelOuterHostStyle = null; Style stItemSkinPanelHostStyle = null; DataTemplate dtItemPanelTemplateHeader = null; if (item.PanelStylingFlags != null) { if (item.PanelStylingFlags.Count > 0) { ResourceDictionary rdItemStyleOverrides = null; string xamlItemStyling = BuildPanelStylingXaml(item.PanelStylingFlags); if (xamlItemStyling != null) { rdItemStyleOverrides = XamlHelper2.ParseXamlResourceDictionary(xamlItemStyling); if (rdItemStyleOverrides != null) { hasStylingFlags = true; stItemSkinPanelOuterHostStyle = XamlHelper2.ResolveStyleFromResourceDictionary(rdItemStyleOverrides, "SkinPanelOuterHostStyle"); stItemSkinPanelHostStyle = XamlHelper2.ResolveStyleFromResourceDictionary(rdItemStyleOverrides, "SkinPanelHostStyle"); dtItemPanelTemplateHeader = XamlHelper2.ResolveStylingDataTemplateFromResourceDictionary(rdItemStyleOverrides, "PanelTemplateHeader"); } } } } if (hasStylingFlags) { item.SkinPanelOuterHostStyle = stItemSkinPanelOuterHostStyle ?? stSkinPanelOuterHostStyle; item.SkinPanelHostStyle = stItemSkinPanelHostStyle ?? stSkinPanelHostStyle; item.ItemHeaderDataTemplate = dtItemPanelTemplateHeader ?? dtPanelTemplateHeader; } else { item.SkinPanelOuterHostStyle = stSkinPanelOuterHostStyle; item.SkinPanelHostStyle = stSkinPanelHostStyle; item.ItemHeaderDataTemplate = dtPanelTemplateHeader; } item.RequestedTheme = bindableLayout.Theme; ContentPresenter cp = new ContentPresenter() { Content = item, ContentTemplate = panelTemplateSelector.SelectTemplate(item), Visibility = item.IsVisible ? Visibility.Visible : Visibility.Collapsed, HorizontalContentAlignment = HorizontalAlignment.Stretch, VerticalContentAlignment = VerticalAlignment.Stretch }; Grid.SetRow(cp, item.Row); Grid.SetColumn(cp, item.Column); Grid.SetRowSpan(cp, item.RowSpan); Grid.SetColumnSpan(cp, item.ColumnSpan); grd.Children.Add(cp); //grd.UpdateLayout(); } } } catch (Exception e) { } return(grd); }