Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TemplateSelectorContentView.cs
1using System;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
8{
13 public class TemplateSelectorContentView : ContentView
14 {
18 public static readonly BindableProperty ItemProperty =
19 BindableProperty.Create(
20 nameof(Item),
21 typeof(object),
23 defaultValue: null,
24 propertyChanged: OnItemChanged);
25
29 public static readonly BindableProperty TemplateSelectorProperty =
30 BindableProperty.Create(
31 nameof(TemplateSelector),
32 typeof(DataTemplateSelector),
34 defaultValue: null,
35 propertyChanged: OnItemChanged);
36
43 public object Item
44 {
45 get => (object)this.GetValue(ItemProperty);
46 set => this.SetValue(ItemProperty, value);
47 }
48
55 public DataTemplateSelector TemplateSelector
56 {
57 get => (DataTemplateSelector)this.GetValue(TemplateSelectorProperty);
58 set => this.SetValue(TemplateSelectorProperty, value);
59 }
60
68 private static void OnItemChanged(BindableObject bindable, object oldValue, object newValue)
69 {
70 if (bindable is TemplateSelectorContentView View)
71 {
72 View.ApplyTemplate();
73 }
74 }
75
80 private void ApplyTemplate()
81 {
82 if (this.TemplateSelector is null || this.Item is null)
83 {
84 this.Content = null;
85 return;
86 }
87
88 DataTemplate? Template = this.TemplateSelector.SelectTemplate(this.Item, this);
89 if (Template is not null)
90 {
91 this.Content = (View)Template.CreateContent();
92 }
93 else
94 {
95 this.Content = null;
96 }
97 }
98 }
99
100}
A custom ContentView control that selects and applies a DataTemplate based on the provided item using...
DataTemplateSelector TemplateSelector
Gets or sets the DataTemplateSelector that will be used to select the appropriate DataTemplate based ...
static readonly BindableProperty ItemProperty
Identifies the Item bindable property.
static readonly BindableProperty TemplateSelectorProperty
Identifies the TemplateSelector bindable property.
object Item
Gets or sets the data item used to determine which template to apply.