2013-01-23 31 views
5

द्वारा पंजीकृत मैं इस कोड है:संपत्ति पहले से ही '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> 

मैं की

'SelectedItemsBehaviour' संपत्ति पहले से ही था एक त्रुटि मिल गया है 'सूचीदृश्य' द्वारा पंजीकृत।

मैंने इस कोड को दो अलग-अलग ListViews पर दो स्थानों पर उपयोग किया है और दोनों ही यह त्रुटि देते हैं।

मुझे यह क्यों मिल रहा है, सबकुछ स्थिर है। मैं एमवीवीएम डिजाइन पैटर्न का उपयोग कर रहा हूँ।

धन्यवाद,

+0

मुझे कॉम्बो बॉक्स व्यवहार के साथ भी यही समस्या थी। [मैंने अपने ब्लॉग में फिक्स का उल्लेख किया है] (http://contractnamespace.blogspot.com/2014/04/default-text-on-wpf-combo-boxes.html), बस अगर कोई और एक जैसा हो त्रुटि। –

उत्तर

12

RegisterAttached (ownerType) को तीसरे पैरामीटर हमेशा वर्ग है कि संपत्ति है, जो यहाँ SelectedItems है वाणी होना चाहिए।

यह एक आम गलतफहमी है कि यह प्रकार संपत्ति का एक संभावित "लक्ष्य" प्रकार है।

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

उत्कृष्ट, धन्यवाद –