Coder Social home page Coder Social logo

Comments (2)

zamby avatar zamby commented on August 10, 2024

I'm trying to change the cell type on the fly.
I try using the FlowTemplateSelector, but does not work.
How can I do?
Thanks.

Simple example:

public class TESTDLTF : ContentView
	{
		public TESTDLTF()
		{
			StackLayout sl = new StackLayout();

			FlowListView hl = new FlowListView();

			TemplateSelector tl = new TemplateSelector();
			tl.template = 1;
			hl.FlowColumnTemplate = tl;
			hl.FlowColumnCount = 1;

			Model model = new Model();
			this.BindingContext = model;

			hl.SetBinding(FlowListView.FlowItemsSourceProperty, "ListViewItems");

			StackLayout stkButtons = new StackLayout() { Orientation = StackOrientation.Horizontal };

			Button b1 = new Button(); b1.Text = "Load";
			b1.Clicked += (sender, e) =>
			{
				model.Load();
			};
			stkButtons.Children.Add(b1);

			Button b2 = new Button(); b2.Text = "Clear";
			b2.Clicked += (sender, e) =>
			{
				model.Clear();
			};
			stkButtons.Children.Add(b2);

			Button b3 = new Button();
			b3.Text = "Change Cell 1";
			b3.Clicked += (sender, e) =>
			{
				tl.template = 0;
				hl.ForceReload();
			};
			stkButtons.Children.Add(b3);

			Button b4 = new Button();
			b4.Text = "Change Cell 2";
			b4.Clicked += (sender, e) =>
			{
				tl.template = 1;
				hl.ForceReload();
			};
			stkButtons.Children.Add(b4);

			sl.Children.Add(stkButtons);
			sl.Children.Add(hl);

			this.Content = sl;

		}

		public class TemplateSelector : FlowTemplateSelector
		{

			public int template { get; set; }

			readonly DataTemplate _cell1 = new DataTemplate(typeof(Cell));
			readonly DataTemplate _cell2 = new DataTemplate(typeof(Cell2));

			protected override DataTemplate OnSelectTemplate(object item, int columnIndex, BindableObject container)
			{
				if (template == 0) return _cell1;
				if (template == 1) return _cell2;
				return null;
			}

		}

		public class Cell : ContentView
		{
			public Cell()
			{
				
				Label lb1 = new Label();
				lb1.SetBinding(Label.TextProperty, "Code");

				StackLayout sl = new StackLayout();
				sl.Children.Add(lb1);

				this.Content = sl;


			}
		}

		public class Cell2 : ContentView
		{
			public Cell2()
			{

				Label lb1 = new Label();
				lb1.SetBinding(Label.TextProperty, "Code");
				Label lb2 = new Label();
				lb2.SetBinding(Label.TextProperty, "Name");

				StackLayout sl = new StackLayout();
				sl.Children.Add(lb1);
				sl.Children.Add(lb2);

				this.Content = sl;


			}
		}

		public class Model: INotifyPropertyChanged
		{

			public event PropertyChangedEventHandler PropertyChanged;
			protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
			{
				PropertyChangedEventHandler handler = PropertyChanged;
				if (handler != null)
				{
					handler(this, new PropertyChangedEventArgs(propertyName));
				}
			}

			public List<Entity> _ListViewItems { get; set; }
			public List<Entity> ListViewItems
			{
				get
				{
					return _ListViewItems;
				}
				set
				{
					_ListViewItems = value;
					OnPropertyChanged("ListViewItems");
				}
			}

			public void Load()
			{
				List<Entity> l = new List<Entity>();
				l.Add(new Entity() { Code = "1", Name = "zxczxczxc" });
				l.Add(new Entity() { Code = "2", Name = "zxczxczxc" });
				l.Add(new Entity() { Code = "3", Name = "zxczxczxc" });
				l.Add(new Entity() { Code = "4", Name = "zxczxczxc" });
				l.Add(new Entity() { Code = "5", Name = "zxczxczxc" });
				l.Add(new Entity() { Code = "6", Name = "zxczxczxc" });
				l.Add(new Entity() { Code = "7", Name = "zxczxczxc" });
				this.ListViewItems = l;
				      
			}

			public void Clear()
			{
				List<Entity> l = new List<Entity>();
				this.ListViewItems = l;
			}

		}

		public class Entity
		{
			public string Code { get; set; }
			public string Name { get; set; }

		}
		
	}

from dltoolkit.forms.controls.

daniel-luberda avatar daniel-luberda commented on August 10, 2024

You should base only on OnSelectTemplate method parameters, item is a BindingContext

public class TemplateSelector : FlowTemplateSelector
{
    readonly DataTemplate _cell1 = new DataTemplate(typeof(Cell));
    readonly DataTemplate _cell2 = new DataTemplate(typeof(Cell2));

    protected override DataTemplate OnSelectTemplate(object item, int columnIndex, BindableObject container)
    {
        if (columnIndex == 0) return _cell1;
        if (columnIndex == 1) return _cell2;
        return null;
    }
}

from dltoolkit.forms.controls.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.