// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Globalization; using System.Reflection; using System.Windows; using System.Windows.Data; namespace Microsoft.Management.UI.Internal { /// /// Provides a way to get the of a visual ancestor. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes")] public class VisualToAncestorDataConverter : IValueConverter { /// /// Searches ancestors for data of the specified class type. /// /// The visual whose ancestors are searched. /// The parameter is not used. /// The type of the data to find. The type must be a class. /// The parameter is not used. /// The data of the specified type; or if not found, null. /// The specified value is not a class type. /// The specified value is a null reference. public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { ArgumentNullException.ThrowIfNull(value); ArgumentNullException.ThrowIfNull(parameter); Type dataType = (Type)parameter; if (dataType.IsClass == false) { throw new ArgumentException("The specified value is not a class type.", "parameter"); } DependencyObject obj = (DependencyObject)value; MethodInfo findVisualAncestorDataMethod = typeof(WpfHelp).GetMethod("FindVisualAncestorData"); MethodInfo genericFindVisualAncestorDataMethod = findVisualAncestorDataMethod.MakeGenericMethod(dataType); return genericFindVisualAncestorDataMethod.Invoke(null, new object[] { obj }); } /// /// This method is not used. /// /// The parameter is not used. /// The parameter is not used. /// The parameter is not used. /// The parameter is not used. /// The parameter is not used. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }