public void RecursiveSettingInSystem () { var tempObjects = new[] { new {Name = "Test1"}, new {Name = "Test2"} }; var template = new DataTemplate (typeof (BindableViewCell)) { Bindings = { {BindableViewCell.NameProperty, new Binding ("Name")} } }; var cell1 = (Cell)template.CreateContent (); cell1.BindingContext = tempObjects[0]; cell1.Parent = new ListView (); var cell2 = (Cell)template.CreateContent (); cell2.BindingContext = tempObjects[1]; cell2.Parent = new ListView (); var viewCell1 = (BindableViewCell) cell1; var viewCell2 = (BindableViewCell) cell2; Assert.AreEqual ("Test1", viewCell1.Name); Assert.AreEqual ("Test2", viewCell2.Name); Assert.AreEqual ("Test1", viewCell1.NameLabel.Text); Assert.AreEqual ("Test2", viewCell2.NameLabel.Text); }
public void Create() { var template = new DataTemplate (typeof(SwitchCell)); var content = template.CreateContent(); Assert.That (content, Is.InstanceOf<SwitchCell>()); }
public void Text() { var template = new DataTemplate (typeof (SwitchCell)); template.SetValue (SwitchCell.TextProperty, "text"); SwitchCell cell = (SwitchCell)template.CreateContent(); Assert.That (cell.Text, Is.EqualTo ("text")); }
public void CreateContentType() { var template = new DataTemplate (typeof (MockBindable)); object obj = template.CreateContent(); Assert.IsNotNull (obj); Assert.That (obj, Is.InstanceOf<MockBindable>()); }
public void On() { var template = new DataTemplate (typeof (SwitchCell)); template.SetValue (SwitchCell.OnProperty, true); SwitchCell cell = (SwitchCell)template.CreateContent(); Assert.That (cell.On, Is.EqualTo (true)); }
public void CreateContentValues() { var template = new DataTemplate (typeof (MockBindable)) { Values = { { MockBindable.TextProperty, "value" } } }; MockBindable bindable = (MockBindable)template.CreateContent(); Assert.That (bindable.GetValue (MockBindable.TextProperty), Is.EqualTo ("value")); }
public void CreateContentBindings() { var template = new DataTemplate (() => new MockBindable()) { Bindings = { { MockBindable.TextProperty, new Binding (".") } } }; MockBindable bindable = (MockBindable)template.CreateContent(); bindable.BindingContext = "text"; Assert.That (bindable.GetValue (MockBindable.TextProperty), Is.EqualTo ("text")); }
public void SetBindingOverridesValue() { var template = new DataTemplate (typeof (MockBindable)); template.SetValue (MockBindable.TextProperty, "value"); template.SetBinding (MockBindable.TextProperty, new Binding (".")); MockBindable bindable = (MockBindable) template.CreateContent(); Assume.That (bindable.GetValue (MockBindable.TextProperty), Is.EqualTo (bindable.BindingContext)); bindable.BindingContext = "binding"; Assert.That (bindable.GetValue (MockBindable.TextProperty), Is.EqualTo ("binding")); }
public void Render() { if (Selector == null || TemplateDictionary == null) { return; } DataTemplate template = SelectTemplate(Selector); var o = template.CreateContent(); if (o != null) { var view = o as View; view.BindingContext = BindingContext; Content = view; } }
/// <summary> /// Inflates the visuals for a data template or template selector /// and adds it to our StackLayout. /// </summary> /// <param name="template">Template.</param> /// <param name="item">Item.</param> View InflateTemplate(DataTemplate template, object item) { // Pull real template from selector if necessary. if (template is DataTemplateSelector dSelector) { template = dSelector.SelectTemplate(item, this); } var view = template.CreateContent() as View; if (view != null) { view.BindingContext = item; } return(view); }
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) { int section = indexPath.Section; int row = indexPath.Row; var context = Groups[section][row]; DataTemplate template = ShellController.GetFlyoutItemDataTemplate(context); if (context is IMenuItemController) { if (DefaultMenuItemTemplate != null && _context.Shell.MenuItemTemplate == template) { template = DefaultMenuItemTemplate; } } else { if (DefaultItemTemplate != null && _context.Shell.ItemTemplate == template) { template = DefaultItemTemplate; } } var cellId = ((IDataTemplateController)template.SelectDataTemplate(context, _context.Shell)).IdString; var cell = (UIContainerCell)tableView.DequeueReusableCell(cellId); if (cell == null) { var view = (View)template.CreateContent(context, _context.Shell); cell = new UIContainerCell(cellId, view); // Set Parent after binding context so parent binding context doesn't propagate to view cell.BindingContext = context; view.Parent = _context.Shell; } else { cell.BindingContext = context; } cell.SetAccessibilityProperties(context); _views[context] = cell.View; return(cell); }
private void OnSelectedItemsCountTemplateChanged(DataTemplate selectedItemsCountTemplate) { if (_selectedItemsCountView != null) { Children.Remove(_selectedItemsCountView); _selectedItemsCountView = null; } if (selectedItemsCountTemplate != null && SelectionMode == DropDownSelectionModes.Multiple) { _selectedItemsCountView = selectedItemsCountTemplate.CreateContent() as View; if (Children.Contains(_selectedItemsCountView) == false) { Children.Add(_selectedItemsCountView); } } }
protected virtual View GetItemView(object item) { var content = ItemTemplate.CreateContent(); var view = content as View; if (view == null) return null; view.BindingContext = item; var gesture = new TapGestureRecognizer { Command = SelectedCommand, CommandParameter = new { ItemsView = this, Item = item } }; AddGesture(view, gesture); return view; }
/// <summary> /// Inflates the visuals for a data template or template selector /// and adds it to our StackLayout. /// </summary> /// <param name="template">Template.</param> /// <param name="item">Item.</param> void InflateTemplate(DataTemplate template, object item) { // Pull real template from selector if necessary. var dSelector = template as DataTemplateSelector; if (dSelector != null) { template = dSelector.SelectTemplate(item, this); } var view = template.CreateContent() as View; if (view != null) { view.BindingContext = item; stack.Children.Add(view); } }
/// <summary> /// Create content with content template /// </summary> private void OnContentTemplateChanged(DataTemplate oldContentTemplate, DataTemplate newContentTemplate) { if (Content == null || Content is View == false) { if (Children.Contains(_actualContentElement)) { Children.Remove(_actualContentElement); } _actualContentElement = newContentTemplate.CreateContent() as View; _actualContentElement.BindingContext = Content; Children.Add(_actualContentElement); } else { // Do nothing with template } }
private void OnStickyHeaderTemplateChanged(DataTemplate newStickyHeaderTemplate) { if (newStickyHeaderTemplate != null && StickyHeader is View == false) { if (_stickyHeaderElement != null && Children.Contains(_stickyHeaderElement)) { Children.Remove(_stickyHeaderElement); } _stickyHeaderElement = newStickyHeaderTemplate.CreateContent() as View; Children.Add(_stickyHeaderElement); if (StickyHeader != null) { _stickyHeaderElement.BindingContext = StickyHeader; } } }
private Layout ViewFromTemplate(DataTemplate template, object bindingContext = null) { View view = null; if (template != null) { view = template.CreateContent() as View; if (!(view is Layout)) { throw new InvalidOperationException("MasterDetail.(Master,Detail)Template.DataTemplate must be a container element dervied from Xamarin.Forms.Layout"); } view.BindingContext = bindingContext; } return(view as Layout); }
private void Create() { this.HeightRequest = GridView.ItemHeight; var items = BindingContext as IEnumerable <object>; int n = items != null?items.Count() : 0; for (int i = 0; i < Slices; i++) { object dataItem = null; if (i < n) { dataItem = items.ElementAt(i); } View view = null; if (i >= Children.Count) { view = DataTemplate.CreateContent() as View; if (view == null) { throw new InvalidOperationException("DataTemplate of Grid must not be of type Cell"); } view.BindingContext = dataItem; ColumnDefinitions.Add(new ColumnDefinition()); Children.Add(view); Grid.SetColumn(view, i); } else { view = Children[i]; view.BindingContext = dataItem; view.IsVisible = dataItem != null; } } int gridChildren = Children.Count; for (int i = n; i < gridChildren; i++) { Children[i].BindingContext = null; Children[i].IsVisible = false; } }
protected override void SetItemTemplate(DataTemplate newValue) { listView.ItemTemplate = new DataTemplate(() => { return(new ViewCell { Height = ItemHeight, View = new GridCell(Slices, new DataTemplate(() => { var content = newValue.CreateContent(); var itemStyle = (AtomListViewCell)(ItemStyleTemplate?.CreateContent() ?? new AtomListViewCell()); itemStyle.Content = (content as View) ?? (content as ViewCell).View; var c = itemStyle; return c; }), this) }); }) ; }
private void OnHeaderTemplateChanged(DataTemplate newHeaderTemplate) { if (newHeaderTemplate != null && Header is View == false) { if (_headerElement != null && Children.Contains(_headerElement)) { Children.Remove(_headerElement); } _headerElement = newHeaderTemplate.CreateContent() as View; Children.Add(_headerElement); IsHeaderVisible = true; if (Header != null) { _headerElement.BindingContext = Header; } } }
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); }
public void SwitchCellSwitchChangedArgs(bool initialValue, bool finalValue) { var template = new DataTemplate(typeof(SwitchCell)); SwitchCell cell = (SwitchCell)template.CreateContent(); SwitchCell switchCellFromSender = null; bool newSwitchValue = false; cell.On = initialValue; cell.OnChanged += (s, e) => { switchCellFromSender = (SwitchCell)s; newSwitchValue = e.Value; }; cell.On = finalValue; Assert.AreEqual(cell, switchCellFromSender); Assert.AreEqual(finalValue, newSwitchValue); }
public void SwitchCellSwitchChangedArgs (bool initialValue, bool finalValue) { var template = new DataTemplate (typeof (SwitchCell)); SwitchCell cell = (SwitchCell)template.CreateContent (); SwitchCell switchCellFromSender = null; bool newSwitchValue = false; cell.On = initialValue; cell.OnChanged += (s, e) => { switchCellFromSender = (SwitchCell)s; newSwitchValue = e.Value; }; cell.On = finalValue; Assert.AreEqual (cell, switchCellFromSender); Assert.AreEqual (finalValue, newSwitchValue); }
private ListView GetSitesListView() { ListView sitesListView = new ListView { ItemsSource = listItems }; sitesListView.ItemAppearing += async(sender, e) => { if (isLoading || listItems.Count == 0 || listItems.Count > 20) { return; } if (e.Item == listItems[listItems.Count - 1]) { await LoadSpSites(); } }; DataTemplate dataTemplate = new DataTemplate(() => new DefaultPageListViewTemplate()); DefaultPageListViewTemplate defaultPageListViewTemplate = dataTemplate.CreateContent() as DefaultPageListViewTemplate; if (defaultPageListViewTemplate != null) { //Do Something here.... } sitesListView.ItemTemplate = dataTemplate; sitesListView.ItemTapped += async(sender, args) => { SpUrlsPerUser spUrlsPerUser = args.Item as SpUrlsPerUser; if (spUrlsPerUser == null || String.IsNullOrEmpty(spUrlsPerUser.Title)) { return; } //Do something here }; return(sitesListView); }
private View CreateTemplatedView(object item, DataTemplate template) { if (template == null) { return(new Label { Text = item.ToString(), BindingContext = item }); } var templatedView = template.CreateContent() as View; if (templatedView == null) { throw new NotSupportedException("Only View based templates are supported."); } templatedView.BindingContext = item; return(templatedView); }
public void Bind(DataTemplate template, object bindingContext, ItemsView itemsView) { var oldElement = VisualElementRenderer?.Element; if (template != _currentTemplate) { // Remove the old view, if it exists if (oldElement != null) { oldElement.MeasureInvalidated -= MeasureInvalidated; itemsView.RemoveLogicalChild(oldElement); ClearSubviews(); _size = Size.Zero; } // Create the content and renderer for the view var view = template.CreateContent() as View; var renderer = TemplateHelpers.CreateRenderer(view); SetRenderer(renderer); } var currentElement = VisualElementRenderer?.Element; // Bind the view to the data item if (currentElement != null) { currentElement.BindingContext = bindingContext; } if (template != _currentTemplate) { // And make the Element a "child" of the ItemsView // We deliberately do this _after_ setting the binding context for the new element; // if we do it before, the element briefly inherits the ItemsView's bindingcontext and we // emit a bunch of needless binding errors itemsView.AddLogicalChild(currentElement); } _currentTemplate = template; }
public static (UIView NativeView, VisualElement FormsElement) RealizeView(object view, DataTemplate viewTemplate, ItemsView itemsView) { if (viewTemplate != null) { // Run this through the extension method in case it's really a DataTemplateSelector viewTemplate = viewTemplate.SelectDataTemplate(view, itemsView); // We have a template; turn it into a Forms view var templateElement = viewTemplate.CreateContent() as View; // Make sure the Visual property is available when the renderer is created PropertyPropagationExtensions.PropagatePropertyChanged(null, templateElement, itemsView); var renderer = GetHandler(templateElement, itemsView.FindMauiContext()); var element = renderer.VirtualView as VisualElement; // and set the view as its BindingContext element.BindingContext = view; return((UIView)renderer.NativeView, element); } if (view is View formsView) { // Make sure the Visual property is available when the renderer is created PropertyPropagationExtensions.PropagatePropertyChanged(null, formsView, itemsView); // No template, and the EmptyView is a Forms view; use that var renderer = GetHandler(formsView, itemsView.FindMauiContext()); var element = renderer.VirtualView as VisualElement; return((UIView)renderer.NativeView, element); } return(new UILabel { TextAlignment = UITextAlignment.Center, Text = $"{view}" }, null); }
private View GetView(DataTemplate templateView, object item = null) { if (templateView == null) { return(new Label { Text = item?.ToString(), Padding = new Thickness(10), VerticalOptions = LayoutOptions.Center, FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)) }); } else if (templateView is DataTemplateSelector selector) { var template = selector.SelectTemplate(item, this); return(template.CreateContent() as View); } else { return(templateView.CreateContent() as View); } }
/// <summary> /// Создает строку для таблицы /// </summary> /// <param name="template">Шаблон который будет использован для создания элемента</param> /// <param name="item">Модель данных</param> /// <param name="container">StackList</param> /// <param name="index">Индекс для автонумерации</param> private static View CreateChildViewFor(DataTemplate template, object item, BindableObject container, int index = -1, int itemsCount = -1) { if (template is DataTemplateSelector selector) { template = selector.SelectTemplate(item, container); } // Здесь происходит неявный вызов метода : DataGridViewCell.CreateView() var view = (View)template.CreateContent(); // Auto number if (index > -1) { var row = (Row)view; if (row.DataGrid.IsAutoNumberCalc) { row.UpdateAutoNumeric(index + 1, itemsCount); } } return(view); }
private GridItem ViewFromTemplate(DataTemplate template, object bindingContext = null) { View view = null; if (template != null) { view = template.CreateContent() as View; if (!(view is Layout)) { throw new InvalidOperationException("GreadRepater.(Item,Header,Footer)Template.DataTemplate must be a container element dervied from Xamarin.Forms.StackLayout"); } var layout = view as GridItem; foreach (var c in layout.Children) { c.BindingContext = bindingContext; } } return(view as GridItem); }
protected virtual View GetItemView(object item) { var content = ItemTemplate.CreateContent(); var view = ((ViewCell)content).View; if (view == null) { return(null); } view.BindingContext = item; var gesture = new TapGestureRecognizer { Command = ItemSelectedCommand, CommandParameter = item }; AddGesture(view, gesture); return(view); }
// This extension method will generate the VisualStateGroups necessary to set a simple background color for // the Selected state on your DataTemplate. // See ConvenienceMethod.xaml.cs for a usage example public static DataTemplate WithSelectedBackgroundColor(this DataTemplate template, Color backgroundColor) { return(new DataTemplate(() => { var content = template.CreateContent() as VisualElement; var backColorSetter = new Setter { Value = backgroundColor, Property = VisualElement.BackgroundColorProperty }; var stateGroup = new VisualStateGroup { Name = "Common", TargetType = typeof(Grid) }; var normalState = new VisualState { Name = "Normal", TargetType = typeof(Grid), Setters = { } }; var selectedState = new VisualState { Name = "Selected", TargetType = typeof(Grid), Setters = { backColorSetter } }; stateGroup.States.Add(normalState); stateGroup.States.Add(selectedState); VisualStateManager.SetVisualStateGroups(content, new VisualStateGroupList { stateGroup }); return content; })); }
public void RecycleCell(object data, DataTemplate dataTemplate, VisualElement parent) { if (_viewCell == null) { //_viewCell = (dataTemplate.CreateContent() as JWChinese.GridViewCell); _viewCell = (dataTemplate.CreateContent() as ViewCell); //reflection method of setting isplatformenabled property // We are going to re - set the Platform here because in some cases (headers mostly) its possible this is unset and // when the binding context gets updated the measure passes will all fail.By applying this here the Update call // further down will result in correct layouts. //var p = PlatformProperty.GetValue(parent); //PlatformProperty.SetValue(_viewCell, p); _viewCell.BindingContext = data; _viewCell.Parent = parent; //_viewCell.PrepareCell(new Size(Bounds.Width, Bounds.Height)); _originalBindingContext = _viewCell.BindingContext; var renderer = Platform.CreateRenderer(_viewCell.View); //RendererFactory.GetRenderer (_viewCell.View); _view = renderer.NativeView; _view.AutoresizingMask = UIViewAutoresizing.All; _view.ContentMode = UIViewContentMode.ScaleToFill; ContentView.AddSubview(_view); return; } else if (data == _originalBindingContext) { _viewCell.BindingContext = _originalBindingContext; } else { _viewCell.BindingContext = data; } }
private View ViewSelector(ISearchItem item) { if (demoDataTemplateSelector) { // this is an example of using a dataTemplate selector int id = int.Parse(item.ID); string dataTemplateKey = id % 2 == 0 ? "TemplateA" : "TemplateB"; if (id % 3 == 0) { dataTemplateKey = "TemplateC"; } DataTemplate dt = this.Resources[dataTemplateKey] as DataTemplate; if (dt != null) { return(dt.CreateContent() as View); } return(null); } else { // this is an example of using a single template for all grid cells return(new MGCheckBoxFilter()); } }
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) { int section = indexPath.Section; int row = indexPath.Row; var context = Groups[section][row]; DataTemplate template = null; if (context is IMenuItemController) { template = Shell.GetMenuItemTemplate(context) ?? _context.Shell.MenuItemTemplate ?? DefaultMenuItemTemplate; } else { template = Shell.GetItemTemplate(context) ?? _context.Shell.ItemTemplate ?? DefaultItemTemplate; } var cellId = ((IDataTemplateController)template.SelectDataTemplate(context, _context.Shell)).IdString; var cell = (UIContainerCell)tableView.DequeueReusableCell(cellId); if (cell == null) { var view = (View)template.CreateContent(context, _context.Shell); view.Parent = _context.Shell; cell = new UIContainerCell(cellId, view); cell.BindingContext = context; } else { cell.BindingContext = context; } _views[context] = cell.View; return(cell); }
StackLayout BuildListView(RootPageViewModel viewModel) { var headerTemplate = new DataTemplate(typeof(ModuleMediaListHeaderTemplate)); headerTemplate.CreateContent(); var itemTemplate = new DataTemplate(typeof(ModuleMediaListItemTemplate)); itemTemplate.CreateContent(); _listView = new ListView { ItemsSource = viewModel.MediaSections, IsGroupingEnabled = true, GroupDisplayBinding = new Binding("SectionName"), HasUnevenRows = false, GroupHeaderTemplate = headerTemplate, ItemTemplate = itemTemplate }; return(new StackLayout { Children = { _listView } }); }
public void SetValueAndBinding () { var template = new DataTemplate (typeof (TextCell)) { Bindings = { {TextCell.TextProperty, new Binding ("Text")} }, Values = { {TextCell.TextProperty, "Text"} } }; Assert.That (() => template.CreateContent (), Throws.InstanceOf<InvalidOperationException> ()); }
public void Detail () { var template = new DataTemplate (typeof (TextCell)); template.SetValue (TextCell.DetailProperty, "detail"); TextCell cell = (TextCell)template.CreateContent (); Assert.That (cell.Detail, Is.EqualTo ("detail")); }
public void Create () { var template = new DataTemplate (typeof (TextCell)); var content = template.CreateContent (); Assert.IsNotNull (content); Assert.That (content, Is.InstanceOf<TextCell> ()); }
private View CreateItemView(object item, DataTemplate itemTemplate) { ItemContainer itemView = new ItemContainer(this) { BindingContext = item, Style = ItemContainerStyle, }; itemView.GestureRecognizers.Add(tabGestureRecognizer); if (itemTemplate == null) { itemView.Content = new Label() { Text = item.ToString() }; } else { View v = (View)itemTemplate.CreateContent(); //FlexBasis basis = FlexLayout.GetBasis(v); //if (basis.Length > 0) //{ // FlexLayout.SetBasis(itemView, basis); //} //FlexAlignSelf alignSelf = FlexLayout.GetAlignSelf(v); //if (alignSelf != FlexAlignSelf.Auto) //{ // FlexLayout.SetAlignSelf(itemView, alignSelf); //} //float grow = FlexLayout.GetGrow(v); //if (grow > 0) //{ // FlexLayout.SetGrow(itemView, grow); //} //float shrink = FlexLayout.GetShrink(v); //if (shrink > 0) //{ // FlexLayout.SetShrink(itemView, shrink); //} //int order = FlexLayout.GetOrder(v); //if (order > 0) //{ // FlexLayout.SetOrder(itemView, order); //} var wrapLayout = Layout as GridWrapLayout; if (wrapLayout != null) { int colSpan = GridWrapLayout.GetColumnSpan(v); if (colSpan > 1) { GridWrapLayout.SetColumnSpan(itemView, colSpan); } int rowSpan = GridWrapLayout.GetRowSpan(v); if (rowSpan > 1) { GridWrapLayout.SetRowSpan(itemView, rowSpan); } } itemView.Content = v; } return(itemView); }
protected virtual View GetItemView(object item) { var content = (ItemTemplate is DataTemplateSelector) ? ((DataTemplateSelector)ItemTemplate).SelectTemplate(item, this).CreateContent() : ItemTemplate.CreateContent(); var view = content as View; if (view == null) { return(null); } view.BindingContext = item; var gesture = new TapGestureRecognizer { Command = SelectedCommand, CommandParameter = new { ItemsView = this, Item = item } }; AddGesture(view, gesture); return(view); }
protected override void Init() { List <GroupedData> groups = new List <GroupedData>(); var group1 = new GroupedData { GroupName = "Group #1" }; group1.Add(new GroupItem { DisplayText = "Text for ListView item 1.1" }); group1.Add(new GroupItem { DisplayText = "Text for ListView item 1.2" }); groups.Add(group1); var group2 = new GroupedData { GroupName = "Group #2" }; group2.Add(new GroupItem { DisplayText = "Text for ListVIew item 2.1" }); group2.Add(new GroupItem { DisplayText = "Text for ListView item 2.2" }); groups.Add(group2); var itemTemplate = new DataTemplate(typeof(GroupItemTemplate)); itemTemplate.CreateContent(); var groupHeaderTemplate = new DataTemplate(typeof(GroupHeaderTemplate)); groupHeaderTemplate.CreateContent(); var listView = new ListView { IsGroupingEnabled = true, GroupDisplayBinding = new Binding("GroupName"), GroupShortNameBinding = new Binding("GroupName"), HasUnevenRows = Device.RuntimePlatform == Device.Android, ItemTemplate = itemTemplate, GroupHeaderTemplate = groupHeaderTemplate, ItemsSource = groups }; Content = new StackLayout { VerticalOptions = LayoutOptions.Center, Children = { new Label { Text = "The group headers below should extend to the width of the screen. If they aren't the width of the screen, this test has failed." }, new ContentView { Content = listView, HorizontalOptions = LayoutOptions.FillAndExpand, Padding = 0 } } }; }