c# – 获取GridViewColumn的父级
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 获取GridViewColumn的父级,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2869字,纯文字阅读大概需要5分钟。
内容图文
![c# – 获取GridViewColumn的父级](/upload/InfoBanner/zyjiaocheng/794/710967f60a054b98b961cb692e9bd0e1.jpg)
有没有办法获得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;
}
}
}
在2009年有7000这个被公开回来,没有任何事情发生,所以我怀疑它会是什么.也就是说,该属性在框架内被广泛使用,并且与其他框架组件相比是Friend Visible,所以我认为这是一个非常安全的选择,它也不会很快改变.
内容总结
以上是互联网集市为您收集整理的c# – 获取GridViewColumn的父级全部内容,希望文章能够帮你解决c# – 获取GridViewColumn的父级所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。