如果我创建多个
ListView
相同的S
ItemsSource
他们之间的联系变得很奇怪。在下面的示例中,这两个
列表视图
s显示字符串的通用列表。断言表明
ItemCollection
S和
SortDescriptionCollection
S是不同的,但如果我尝试对
列表视图
不同的是,第二个排序顺序同时适用于这两个。
两个
项目集
s必须是相关的,以便
Selector.IsSynchronizedWithCurrentItem
属性具有任何效果,但我希望能够断开此关联,以便可以执行本例中尝试的操作。是否有人知道这些集合是如何关联的,以及我如何切断这种关系?
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>
代码落后:
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));
}
}
}