我有一个ListViewItem添加到ListView,但我不知道它添加到哪个ListView.
我想(通过ListViewItem)能够从项目本身获取ListView.
我尝试使用Parent属性,但由于某种原因,它返回一个StackPanel.
有任何想法吗?
我已经让它运行和工作:
private void Window_Loaded(object s, RoutedEventArgs args)
{
var collectionview = CollectionViewSource.GetDefaultView(this.listview.Items);
collectionview.CollectionChanged += (sender, e) =>
{
if (e.NewItems != null && e.NewItems.Count > 0)
{
var added = e.NewItems[0];
ListViewItem item = added as ListViewItem;
ListView parent = FindParent<ListView>(item);
}
};
}
public static T FindParent<T>(FrameworkElement element) where T : FrameworkElement
{
FrameworkElement parent = LogicalTreeHelper.GetParent(element) as FrameworkElement;
while (parent != null)
{
T correctlyTyped = parent as T;
if (correctlyTyped != null)
return correctlyTyped;
else
return FindParent<T>(parent);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5006 次 |
| 最近记录: |