I want to create a converter in which I will pass the type to search in the parent hierarchy of elements, and it should return true if such a type is found, otherwise false.
So far I have tried the code below and it works. but now only I have a problem: it finds the hierarchy of the parent elements until the parent of the element is zero. I want to give an ancestor level to search for an element in the parent hierarchy. So how can I predetermine the level of the predecessor?
I used the LayoutHelper.cs Class to search for an item in the parent hierarchy, as shown below.
public class LayoutHelper { public static FrameworkElement FindElement(FrameworkElement treeRoot, Type type, int AncestorLevel) { FrameworkElement parentElement = VisualTreeHelper.GetParent(treeRoot) as FrameworkElement; int level = 1; while (parentElement != null && level <= AncestorLevel) { if (parentElement.GetType() == type) return parentElement; else parentElement = VisualTreeHelper.GetParent(parentElement) as FrameworkElement; level++; } return null; } }
IsTypeFoundConverter.cs: -
public class IsTypeFoundConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { FrameworkElement element = value as FrameworkElement; Type type = parameter as Type; if (element != null && type != null) { element = LayoutHelper.FindElement(element, type,5); if (element != null) return true; } return false; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } }
DataTrigger with IsTypeFoundConverter:
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self} ,Converter={StaticResource isTypeFoundConverter}, ConverterParameter={x:Type Type_To_Find}}" Value="false"> </DataTrigger>
Here, in the data trigger, how can I pass AncestorLevel to the converter so that I can only find the element to this level
Thanks Amol Bavannavar