ListView的项目集合是如何关联的?

时间:2021-01-05 15:51:56

If I create multiple ListViews with the same ItemsSource they become strangely linked. In the following example, the two ListViews display a common list of strings. The assertions show that the two ItemCollections and SortDescriptionCollections are distinct, but if I attempt to sort the ListViews differently, the second sort order is applied to both.

如果我用相同的项目源创建多个列表视图,它们会奇怪地链接在一起。在下面的示例中,两个listview显示一个公共的字符串列表。断言表明这两个ItemCollections和SortDescriptionCollections是不同的,但是如果我尝试对listview进行不同的排序,则对这两个listview应用了第二个排序顺序。

The two ItemCollections must be related in order for the Selector.IsSynchronizedWithCurrentItem property to have any effect, but I would like to be able to break this association so that I can do things like I've tried in this example. Does anyone know how these collections are related, and how I can sever this relationship?

为了选择器,这两个项集合必须是相关的。issynchronizedem属性具有任何效果,但是我希望能够打破这种关联,这样我就可以像在这个示例中那样做一些事情。有人知道这些收藏是如何关联的吗?我怎样才能断绝这种关系?

XAML:

XAML:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:llv="clr-namespace:LinkedListViews"
        x:Class="LinkedListViews.Window1"
        x:Name="Window"
        Title="Window1"
        Width="640" Height="480">

    <Grid x:Name="LayoutRoot">
        <ListView 
                x:Name="ListView1"
                ItemsSource="{Binding ElementName=Window, Path=Data}" 
                Margin="75,8,0,8" Width="237" HorizontalAlignment="Left"/>
        <ListView 
                x:Name="ListView2"
                ItemsSource="{Binding ElementName=Window, Path=Data}" 
                HorizontalAlignment="Right" Margin="0,8,73,8" Width="243"/>
    </Grid>
</Window>

Code behind:

背后的代码:

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.ComponentModel;
using System.Collections.Generic;

namespace LinkedListViews
{
    public partial class Window1
    {
        private List<string> _Data = new List<string>
        { 
            "Alpha", "Beta", "Gamma"
        };
        public List<string> Data
        {
            get { return _Data; }
        }

        public Window1()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
            System.Diagnostics.Debug.Assert(ListView1.Items != ListView2.Items);
            System.Diagnostics.Debug.Assert(ListView1.Items.SortDescriptions != ListView2.Items.SortDescriptions);
            this.ListView1.Items.SortDescriptions.Add(new SortDescription(null, ListSortDirection.Ascending));
            this.ListView2.Items.SortDescriptions.Clear();
            this.ListView2.Items.SortDescriptions.Add(new SortDescription(null, ListSortDirection.Descending));
        }
    }
}

1 个解决方案

#1


1  

See Stopping ItemsControls from sharing filters

从共享过滤器中查看停止项目控制。

#1


1  

See Stopping ItemsControls from sharing filters

从共享过滤器中查看停止项目控制。