How to create a converter to work like "FindAncestor" for checking IsTypeFound?

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"> <!--Setters--> </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

0
source share
1 answer

Declare a type containing converter parameters:

 public sealed class MyConverterParameter { public Type AncestorType { get; set; } public int AncestorLevel { get; set; } } 

Overwrite converter to use this type:

 public sealed class MyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var myParameter = (MyConverterParameter)parameter; // other converter stuff here } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { var myParameter = (MyConverterParameter)parameter; // other converter stuff here } } 

Declare an additional resource in XAML, for example:

  <DataTemplate> <DataTemplate.Resources> <local:MyConverter x:Key="MyConverterKey"/> <local:MyConverterParameter x:Key="MyConverterParameterKey" AncestorType="{x:Type Type-To-Find}" AncestorLevel="1"/> </DataTemplate.Resources> <!-- DataTemplate tree --> <DataTemplate.Triggers> <DataTrigger Binding="{Binding Path-To-Bind-To, Converter={StaticResource MyConverterKey}, ConverterParameter={StaticResource MyConverterParameterKey}}" Value="Value-To-Fire-Trigger"> <!-- Setters --> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> 

PS Why FindAncestor mode for RealtiveSource not suit you?

+1
source

Source: https://habr.com/ru/post/980036/


All Articles