2013-01-23 26 views
5

ho questo codice:Proprietà già registrato da 'ListView'

using System.Collections; 
using System.Windows; 
using System.Windows.Controls; 

public static class SelectedItems 
{ 
    private static readonly DependencyProperty SelectedItemsBehaviorProperty = DependencyProperty.RegisterAttached(
     "SelectedItemsBehavior", 
     typeof(SelectedItemsBehavior), 
     typeof(ListView), 
     null); 

    public static readonly DependencyProperty ItemsProperty = DependencyProperty.RegisterAttached(
     "Items", 
     typeof(IList), 
     typeof(SelectedItems), 
     new PropertyMetadata(null, ItemsPropertyChanged)); 

    public static void SetItems(ListView listView, IList list) 
    { 
     listView.SetValue(ItemsProperty, list); 
    } 

    public static IList GetItems(ListView listView) 
    { 
     return listView.GetValue(ItemsProperty) as IList; 
    } 

    private static void ItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var target = d as ListView; 
     if (target != null) 
     { 
      CreateBehavior(target, e.NewValue as IList); 
     } 
    } 

    private static void CreateBehavior(ListView target, IList list) 
    { 
     var behavior = target.GetValue(SelectedItemsBehaviorProperty) as SelectedItemsBehavior; 
     if (behavior == null) 
     { 
      behavior = new SelectedItemsBehavior(target, list); 
      target.SetValue(SelectedItemsBehaviorProperty, behavior); 
     } 
    } 
} 

public class SelectedItemsBehavior 
{ 
    private readonly ListView _listView; 
    private readonly IList _boundList; 

    public SelectedItemsBehavior(ListView listView, IList boundList) 
    { 
     _boundList = boundList; 
     _listView = listView; 
     _listView.SelectionChanged += OnSelectionChanged; 
    } 

    private void OnSelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     _boundList.Clear(); 

     foreach (var item in _listView.SelectedItems) 
     { 
      _boundList.Add(item); 
     } 
    } 
} 

XAML:

<ListView SelectionMode="Extended" ItemsSource="{Binding Computers}" Views:SelectedItems.Items="{Binding SelectedComputers}" BorderBrush="Transparent" Height="100" VerticalAlignment="Top"> 
     <ListView.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" /> 
      </ItemsPanelTemplate> 
     </ListView.ItemsPanel> 
     <ListView.Background> 
      <SolidColorBrush Color="Black" /> 
     </ListView.Background> 
    </ListView> 

ho un errore di

proprietà 'SelectedItemsBehaviour' era già registrato da 'Listview'.

Ho usato questo codice in due punti su due distinti ListViews ed entrambi danno questo errore.

Perché lo ottengo, tutto è statico. Sto usando il modello di progettazione MVVM.

Grazie,

+0

Ho anche avuto lo stesso problema con un comportamento di casella combinata. [Ho menzionato la correzione nel mio blog] (http://contractnamespace.blogspot.com/2014/04/default-text-on-wpf-combo-boxes.html), nel caso in cui qualcun altro si imbattesse nello stesso errore. –

risposta

12

Il terzo parametro di RegisterAttached (ownerType) deve essere sempre la classe che dichiara la proprietà, che è SelectedItems qui.

È un comune equivoco che questo tipo sia un potenziale tipo "target" della proprietà.

private static readonly DependencyProperty SelectedItemsBehaviorProperty = DependencyProperty.RegisterAttached(
    "SelectedItemsBehavior", 
    typeof(SelectedItemsBehavior), 
    typeof(SelectedItems), // here 
    null); 
+0

Eccellente, grazie –

Problemi correlati