Skip to content

Instantly share code, notes, and snippets.

@exyi
Created July 13, 2016 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exyi/111d7072e30c169d6d6bd5cc8944c168 to your computer and use it in GitHub Desktop.
Save exyi/111d7072e30c169d6d6bd5cc8944c168 to your computer and use it in GitHub Desktop.
simple server rendred TreeView control for dotvvm
public interface IHierarchyItem
{
IEnumerable<IHierarchyItem> Children { get; }
}
public class TreeView : DotvvmControl
{
public IEnumerable<IHierarchyItem> DataSource
{
get { return (IEnumerable<IHierarchyItem>)GetValue(DataSourceProperty); }
set { SetValue(DataSourceProperty, value); }
}
public static readonly DotvvmProperty DataSourceProperty
= DotvvmProperty.Register<IEnumerable<IHierarchyItem>, TreeView>(c => c.DataSource, null);
// change DataContext in ItemTemplate to DataSource element type
[ControlPropertyBindingDataContextChange(nameof(DataSource), order: 0)]
[CollectionElementDataContextChange(order: 1)]
[MarkupOptions(MappingMode = MappingMode.InnerElement)]
public ITemplate ItemTemplate { get; set; }
public void DataBind(IDotvvmRequestContext context)
{
Children.Clear();
foreach (var item in DataSource)
{
DataBindItem(this, item, context);
}
}
public void DataBindItem(DotvvmControl parent, IHierarchyItem item, IDotvvmRequestContext context)
{
// render item template
var templatePlaceholder = new PlaceHolder();
templatePlaceholder.DataContext = item;
parent.Children.Add(templatePlaceholder);
ItemTemplate.BuildContent(context, templatePlaceholder);
if (item.Children.Any())
{
// wrap children in div
var childContainer = new HtmlGenericControl("div");
childContainer.Attributes["class"] = "child-container";
foreach (var child in item.Children)
{
DataBindItem(childContainer, child, context);
}
}
}
protected internal override void OnLoad(IDotvvmRequestContext context)
{
DataBind(context);
base.OnInit(context);
}
protected internal override void OnPreRender(IDotvvmRequestContext context)
{
DataBind(context);
base.OnLoad(context);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment