gpt4 book ai didi

c# - 获取 GridViewColumn 的父级

转载 作者:太空狗 更新时间:2023-10-30 01:20:00 25 4
gpt4 key购买 nike

有没有办法获取 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();
}
}
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();
}
}
public class Ancestor<T>
{
public T Instance { get; set; }
}

最佳答案

不幸的是,您想要的内容隐藏在 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;
}
}
}

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

关于c# - 获取 GridViewColumn 的父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20985005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com