Created
February 1, 2018 18:24
-
-
Save tmyt/6dd13ad3ee430e97b2e264d03861698d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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