Recursive ContainerFromItem
if (dp != null)
return dp;
foreach (var item in itemsControl.Items)
{
var currentTreeViewItem = itemsControl.ItemContainerGenerator.ContainerFromItem(item);
var childDp = ContainerFromItem(currentTreeViewItem as ItemsControl, value);
if (childDp != null)
return childDp;
}
return null;
}</pre>
public static class ItemsControlExtension { public static DependencyObject ContainerFromItem(this ItemsControl itemsControl, object value) { var dp = itemsControl.ItemContainerGenerator.ContainerFromItem(value);
if (dp != null)
return dp;
foreach (var item in itemsControl.Items)
{
var currentTreeViewItem = itemsControl.ItemContainerGenerator.ContainerFromItem(item);
var childDp = ContainerFromItem(currentTreeViewItem as ItemsControl, value);
if (childDp != null)
return childDp;
}
return null;
}
public static T ContainerFromItem<T>(this ItemsControl itemsControl, object value) where T : class
{
return ContainerFromItem(itemsControl, value) as T;
}
}
public static class SelectorExtension { public static DependencyObject ContainerFromSelectedItem(Selector selector) { var selectedItem = selector.SelectedItem;
if (selectedItem == null)
return null;
return selector.ContainerFromItem(selectedItem);
}
public static T ContainerFromSelectedItem<T>(Selector selector) where T : class
{
return ContainerFromSelectedItem(selector) as T;
}
}
public static class TreeViewExtension { public static void ClearSelection(this TreeView treeview) { var selectedItem = ContainerFromSelectedItem<TreeViewItem>(treeview);
if (selectedItem == null) return;
selectedItem.IsSelected = false;
}
public static DependencyObject ContainerFromSelectedItem(this TreeView treeview)
{
var selectedItem = treeview.SelectedItem;
if (selectedItem == null)
return null;
return treeview.ContainerFromItem(selectedItem);
}
public static T ContainerFromSelectedItem<T>(this TreeView treeview) where T : class
{
return ContainerFromSelectedItem(treeview) as T;
}
}