tôi có mã này:tài sản đã đăng ký bởi '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>
Tôi đã có một lỗi của
tài sản 'SelectedItemsBehaviour' là đã được đăng ký bởi 'Chế độ xem danh sách'.
Tôi đã sử dụng mã này ở hai nơi trên hai riêng biệt ListViews
và cả hai đều đưa ra lỗi này.
Tại sao tôi nhận được nó, mọi thứ đều tĩnh. Tôi đang sử dụng mẫu thiết kế MVVM.
Cảm ơn,
Tôi cũng gặp vấn đề tương tự với hành vi hộp tổ hợp. [Tôi đã đề cập đến bản sửa lỗi trong blog của tôi] (http://contractnamespace.blogspot.com/2014/04/default-text-on-wpf-combo-boxes.html), chỉ trong trường hợp nếu có ai khác cùng xem lỗi. –