Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何从ListViewItem获取ListView?_Wpf - Fatal编程技术网

Wpf 如何从ListViewItem获取ListView?

Wpf 如何从ListViewItem获取ListView?,wpf,Wpf,我有一个添加到ListView的ListViewItem,但我不知道它添加到了哪个ListView 我希望(通过ListViewItem)能够从项目本身获取ListView 我尝试使用Parent属性,但由于某种原因,它返回一个StackPanel 有什么想法吗?我已经准备好运行和工作了: private void Window_Loaded(object s, RoutedEventArgs args) { var collectionview = Collection

我有一个添加到ListView的ListViewItem,但我不知道它添加到了哪个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;
    }
private void Window\u已加载(对象、路由目标参数)
{
var collectionview=CollectionViewSource.GetDefaultView(this.listview.Items);
collectionview.CollectionChanged+=(发件人,e)=>
{
如果(e.NewItems!=null&&e.NewItems.Count>0)
{                   
增加的var=e.NewItems[0];
ListViewItem=添加为ListViewItem;
ListView父项=FindParent(项);
}
};
}   
公共静态T FindParent(FrameworkElement元素),其中T:FrameworkElement
{
FrameworkElement parent=LogicalTreeHelper.GetParent(元素)作为FrameworkElement;
while(父级!=null)
{
T correctlyTyped=父项为T;
if(correctlyTyped!=null)
返回正确类型;
其他的
返回FindParent(父级);
}
返回null;
}

虽然这是一个很老的问题,但它对WinRT不起作用


对于WinRT,您需要使用VisualTreeHelper而不是LogicalTreeHelper遍历可视化树,以从ListViewItem中查找ListView。我使用的方法与已经建议的方法不同

我只有少数几个ListView控件(两个或三个),因此我可以执行以下操作

ListViewItem listViewItem = e.OriginalSource as ListViewItem;
if (listViewItem == null)
{
    ...
}
else
{
    if (firstListView.ItemContainerGenerator.IndexFromContainer(listViewItem) >= 0)
    {
        ...
    }
    else if (secondListView.ItemContainerGenerator.IndexFromContainer(listViewItem) >= 0)
    {
        ...
    }
}

这可以与foreach循环一起使用,但是如果有数百个ListView控件需要迭代,那么查找ListViewItem的父ListView可能更有效(正如大多数其他答案所建议的)。然而,我认为我的解决方案更干净(有点)。希望它能帮助别人

我最近发现了这个简洁的解决方案:

ListView listView = ItemsControl.ItemsControlFromItemContainer(listViewItem) as ListView;

MVVM没有这样的问题。就像这与此有关…当LogicalTreeHelper.GetParent(item)刚刚给我“null”时,这就起作用了