有没有办法获取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; }
}
答案 0 :(得分:4)
不幸的是,您想要隐藏在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对于其他框架程序集,所以我认为这是一个非常安全的选择,它也不会很快改变。
答案 1 :(得分:0)
我知道这是3年前被问到的,但我现在需要做几次这样的事情,而且我总是不断地回答这个问题,所以我认为有这个问题的其他人会遇到同样的问题。
我找到了一个简单的解决方案和类似的问题,只要您在listview或gridview的父对象实例化时愿意添加一些代码。
长话短说:我利用.NET System.Collections.Generic.Dictionary功能,因为我创建了这样一个字典(“我被告知的对象类型”,“我需要检索的对象类型”),例如Dictionary(Of GridViewColumn,ListView) - 并在父对象的实例化中加载它。如果我那么,比如说,得到一个GridViewColumn,我需要知道它的“父”列表视图是什么,我只需要参考这个词典来获取它。
对我来说似乎是工作:)