5

我有这个代码:

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”属性已由“Listview”注册。

我已经在两个不同的地方使用了这个代码,ListViews并且都给出了这个错误。

为什么我得到它,一切都是静态的。我正在使用 MVVM 设计模式。

谢谢,

4

1 回答 1

12

RegisterAttached ( )的第三个参数ownerType必须始终是声明属性的类,即SelectedItems此处。

一种常见的误解是,这种类型是属性的潜在“目标”类型。

private static readonly DependencyProperty SelectedItemsBehaviorProperty = DependencyProperty.RegisterAttached(
    "SelectedItemsBehavior",
    typeof(SelectedItemsBehavior),
    typeof(SelectedItems), // here
    null);
于 2013-01-23T11:02:20.323 回答