I'm using ItemTemplateSelector method to differentiate between the templates displayed for level 0 and 1, and also try to use the same concept to further differentiate between templates used inside a level when item is selected or not. In short, in UWP and iOS everything seems to be alright but not in Android where, when returning the templates for selected / deselected states, everything from the lines subject to selection /deselection vanishes.
The ItemTemplateSelector looks this way:
public class ItemTemplateSelector : DataTemplateSelector
{
public DataTemplate Template1 { get; set; }
public DataTemplate Template2 { get; set; }
public DataTemplate Template3 { get; set; }
public DataTemplate Template4 { get; set; }
public ItemTemplateSelector()
{
Template1 = new DataTemplate(typeof(TemplateLevel0));
Template2 = new DataTemplate(typeof(TemplateSelectLevel0));
Template3 = new DataTemplate(typeof(TemplateLevel1));
Template4 = new DataTemplate(typeof(TemplateSelectLevel1));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var treeviewNode = item as TreeViewNode;
if (treeviewNode == null)
return null;
Boolean IsChecked = (Boolean)treeviewNode.IsChecked;
switch (treeviewNode.Level)
{
case 0:
if (IsChecked)
{
return Template2;
}
else
{
return Template1;
}
case 1:
if (IsChecked)
{
return Template4;
}
else
{
return Template3;
}
default:
return null;
}
}
}
The idea behind is to add the selected line /item in the treeview as "checked" in the CheckedItems list using this code:
private void treeView_SelectionChanged(object sender, ItemSelectionChangedEventArgs e)
{
_ExecuteItemSelection = false;
}
private async void treeView_SelectionChanging(object sender, ItemSelectionChangingEventArgs e)
{
if (_ExecuteItemDoubleTapped) return;
if (_ExecuteItemSelection) return;
_ExecuteItemSelection = true;
var elem = e.AddedItems.FirstOrDefault();
devaptreeView.CheckedItems.Clear();
if (elem != null) devaptreeView.CheckedItems.Add(elem);
}
Although looks unorthodox, works well in UWP and iOS, but not in Android. I all 3 OS's the IsChecked variable is correctly detected at the ItemTemplateSelector level, but some strange things happen when returning templates in Android. If you are not trying to select something, in Android, the level based template selection in ItemTemplateSelector is working as expected, only when executing the if (IsChecked) branch the code misbehave.
Thank you