Skip to content

Instantly share code, notes, and snippets.

@se5a
Created September 24, 2016 19:21
Show Gist options
  • Save se5a/f59b63b45ee43f8506aed0bc24e315be to your computer and use it in GitHub Desktop.
Save se5a/f59b63b45ee43f8506aed0bc24e315be to your computer and use it in GitHub Desktop.
Eto GenericControlStack
using System;
using Eto.Forms;
using Eto.Serialization.Xaml;
using System.Collections.Specialized;
using System.Collections;
namespace Pulsar4X.CrossPlatformUI.Views
{
public class GenericStackControl : Panel
{
protected StackLayout Stack;
internal Type ControlType { get; set; } = typeof(Label);
public GenericStackControl()
{
XamlReader.Load(this);
DataContextChanged += GenericStackControl_DataContextChanged;
}
private void GenericStackControl_DataContextChanged(object sender, EventArgs e)
{
Type dcType = DataContext.GetType();
if (DataContext is ICollection)
{
var collection = (ICollection)DataContext;
Stack.Items.Clear();
foreach (var item in collection)
{
Control ctrl = (Control)Activator.CreateInstance(ControlType);
ctrl.DataContext = item;
Stack.Items.Add(ctrl);
}
if (DataContext is INotifyCollectionChanged)
{
var objcollection = (INotifyCollectionChanged)DataContext;
objcollection.CollectionChanged += Collection_CollectionChanged;
}
}
}
private void Collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment