Skip to content

Instantly share code, notes, and snippets.

@tmyt
Created February 1, 2018 18:24
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 tmyt/6dd13ad3ee430e97b2e264d03861698d to your computer and use it in GitHub Desktop.
Save tmyt/6dd13ad3ee430e97b2e264d03861698d to your computer and use it in GitHub Desktop.
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Microsoft.Xaml.Interactivity;
namespace Tetraptera.Views.Behaviors
{
public class SelectTemplateBehavior : DependencyObject, IBehavior
{
public void Attach(DependencyObject associatedObject)
{
if (associatedObject is ListView list)
{
list.ChoosingItemContainer += OnChoosingItemContainer;
}
}
public void Detach()
{
if (AssociatedObject is ListView list)
{
list.ChoosingItemContainer -= OnChoosingItemContainer;
}
}
private void OnChoosingItemContainer(ListViewBase sender, ChoosingItemContainerEventArgs args)
{
var template = Selector.SelectTemplate(args.Item);
if (args.ItemContainer != null && args.ItemContainer.ContentTemplate != template)
{
args.ItemContainer.ContentTemplate = template;
}
if (args.ItemContainer == null)
{
args.ItemContainer = new ListViewItem
{
Style = sender.ItemContainerStyle,
ContentTemplate = template,
};
}
args.IsContainerPrepared = true;
}
public DependencyObject AssociatedObject { get; }
public DataTemplateSelector Selector { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment