2009-07-01 5 views
2

Si je crée plusieurs ListView s avec le même ItemsSource ils deviennent étrangement liés. Dans l'exemple suivant, les deux ListView affichent une liste commune de chaînes. Les assertions montrent que les deux ItemCollection et SortDescriptionCollection s sont distincts, mais si je tente de trier les ListView s différemment, le deuxième ordre de tri est appliqué aux deux.Comment sont liés les ItemCollections de ListView?

Les deux ItemCollection doivent être liés pour que la propriété Selector.IsSynchronizedWithCurrentItem ait un effet, mais j'aimerais pouvoir annuler cette association pour pouvoir faire des choses comme j'ai essayé dans cet exemple. Est-ce que quelqu'un sait comment ces collections sont liées, et comment je peux rompre cette relation?

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 derrière:

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)); 
     } 
    } 
} 

Répondre

Questions connexes