[英]Get parent for GridViewColumn
有沒有辦法獲得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年被公之於眾,並沒有發生任何事情,所以我懷疑它會是什么。 也就是說,該屬性在框架中被廣泛使用,並且對其他框架程序集是友好可見的 ,所以我認為這是一個非常安全的選擇它也不會很快改變。
我知道這是3年前被問到的,但我現在需要做幾次這樣的事情,我總是不斷地回答這個問題,所以我認為其他人遇到這個問題會有同樣的問題。
我找到了一個簡單的解決方案和類似的問題,只要你在listview或gridview的父對象實例化時願意添加一些代碼。
簡而言之:我利用.NET System.Collections.Generic.Dictionary功能,我創建了這樣一個字典(“我被告知的對象類型”,“我需要檢索的對象類型”),例如Dictionary(Of GridViewColumn) ,ListView) - 並在父對象的實例化中加載它。 如果我然后,比如說,得到一個GridViewColumn,我需要知道它的“父”列表視圖是什么,我只是引用這個詞典來獲取它。
似乎是為了我的工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.