获取 GridViewColumn 的父级

Joh*_*son 5 c# wpf gridview attached-properties

有没有办法获取 GridViewColumn 的父级(ListView)?

我尝试过 LogicalTreeHelper 和 VisualTreeHelper,但没有骰子。

我可以分享一个有点有趣的你尝试过的东西,它有效但丑陋并不能描述它:

public class Prototype
{
    [Test, RequiresSTA]
    public void HackGetParent()
    {
        var lw = new ListView();
        var view = new GridView();
        var gvc = new GridViewColumn();
        view.Columns.Add(gvc);
        lw.View = view;
        var ancestor = new Ancestor<ListView>();

        var binding = new Binding
        {
            RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ListView), 1),
            Converter = new GetAncestorConverter<ListView>(), // Use converter to hack out the parent
            ConverterParameter = ancestor // conveterparameter used to return the parent
        };
        BindingOperations.SetBinding(gvc, GridViewColumn.WidthProperty, binding);

        lw.Items.Add(DateTime.Now); // think it cannot be empty for resolve to work
        ResolveBinding(lw);
        Assert.AreEqual(lw, ancestor.Instance);
    }

    private void ResolveBinding(FrameworkElement element)
    {
        element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        element.Arrange(new Rect(element.DesiredSize));
        element.UpdateLayout();
    }
}
Run Code Online (Sandbox Code Playgroud)
public class GetAncestorConverter<T> : IValueConverter
{
    public object Convert(object value, Type type, object parameter, CultureInfo culture)
    {
        var ancestor = (Ancestor<T>)parameter;
        ancestor.Instance = (T)value;
        return null;
    }

    public object ConvertBack(object o, Type type, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
Run Code Online (Sandbox Code Playgroud)
public class Ancestor<T>
{
    public T Instance { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*wen 5

不幸的是,您想要的东西隐藏在 的内部属性后面,DependencyObject InheritanceContext因此访问它的唯一方法是通过反射。因此,如果您对此感到满意,那么此解决方案将起作用。

public class Prototype
{
    [Test, RequiresSTA]
    public void HackReflectionGetParent()
    {
        var lw = new ListView();
        var view = new GridView();
        var gvc = new GridViewColumn();
        view.Columns.Add(gvc);
        lw.View = view;

        var resolvedLw = gvc.GetParents().OfType<ListView>().FirstOrDefault();
        Assert.AreEqual(lw, resolvedLw);
    }
}

public static class DependencyObjectExtensions
{
    private static readonly PropertyInfo InheritanceContextProp = typeof (DependencyObject).GetProperty("InheritanceContext", BindingFlags.NonPublic | BindingFlags.Instance);

    public static IEnumerable<DependencyObject> GetParents(this DependencyObject child)
    {
        while (child != null)
        {
            var parent = LogicalTreeHelper.GetParent(child);
            if (parent == null)
            {
                if (child is FrameworkElement)
                {
                    parent = VisualTreeHelper.GetParent(child);
                }
                if (parent == null && child is ContentElement)
                {
                    parent = ContentOperations.GetParent((ContentElement) child);
                }
                if (parent == null)
                {
                    parent = InheritanceContextProp.GetValue(child, null) as DependencyObject;
                }
            }
            child = parent;
            yield return parent;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

一些讨论关于这个被公开早在2009年和什么都没有发生,所以我怀疑这将是。也就是说,该属性在框架内被广泛使用,并且对其他框架程序集是Friend Visible,所以我认为这是一个非常安全的赌注,它也不会很快改变。