gpt4 book ai didi

mvvm - 使用 MVVM 模式在 Telerik RadGridView 中查找选定项目

转载 作者:行者123 更新时间:2023-12-01 07:44:04 24 4
gpt4 key购买 nike

我在我的应用程序中使用 Telerik RadGridView,它有一个 GridViewSelectColumn 项目,它允许我选择网格中的各种项目。我有一个操作此选择的按钮,但不确定如何获取所选项目的列表。问题是我在 Caliburn.Micro 中使用了 MVVM 模式。是否需要在 View 中找到控件并遍历选中项列表?对于一个简单的任务来说,这似乎需要做很多工作。我会很感激任何想法。

最佳答案

Telerik 的 RadGridView 的问题在于,它的 SelectedItem 集合是只读的,因此您不能将双向绑定(bind)到 SelectedItems。

解决方法是使用自定义行为在 RadGridView 和您的 ViewModels SelectedItem 集合之间进行同步

您可以使用此行为:

// Behavior for synchronizing a RadDataGrid's SelectedItems collection with a SelectedItems collection of the ViewModel (the Network)
// The problem is, that RadDataGrid.SelectedItems is a read-only collection and therefore cannot be used for two-way binding.

class SelectedSyncBehavior
: Behavior<RadGridView>
{
bool _collectionChangedSuspended = false;

protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged;
}

/// <summary>
/// Getter/Setter for DependencyProperty, bound to the DataContext's SelectedItems ObservableCollection
/// </summary>
public INotifyCollectionChanged SelectedItems
{
get { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}

/// <summary>
/// Dependency Property "SelectedItems" is target of binding to DataContext's SelectedItems
/// </summary>
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(INotifyCollectionChanged), typeof(SelectedSyncBehavior), new PropertyMetadata(OnSelectedItemsPropertyChanged));

/// <summary>
/// PropertyChanged handler for DependencyProperty "SelectedItems"
/// </summary>
private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
INotifyCollectionChanged collection = args.NewValue as INotifyCollectionChanged;
if (collection != null)
{
// Hook to the Network's SelectedItems
collection.CollectionChanged += (target as SelectedSyncBehavior).ContextSelectedItems_CollectionChanged;
}
}

/// <summary>
/// Will be called, when the Network's SelectedItems collection changes
/// </summary>
void ContextSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_collectionChangedSuspended) return; // Don't react recursively to CollectionChanged events

_collectionChangedSuspended = true;

// Select and unselect items in the grid
if (e.NewItems != null)
foreach (object item in e.NewItems)
AssociatedObject.SelectedItems.Add(item);

if (e.OldItems != null)
foreach (object item in e.OldItems)
AssociatedObject.SelectedItems.Remove(item);

_collectionChangedSuspended = false;
}

/// <summary>
/// Will be called when the GridView's SelectedItems collection changes
/// </summary>
void GridSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_collectionChangedSuspended) return; // Don't react recursively to CollectionChanged events

_collectionChangedSuspended = true;

// Select and unselect items in the DataContext
if (e.NewItems != null)
foreach (object item in e.NewItems)
(SelectedItems as IList).Add(item);

if (e.OldItems != null)
foreach (object item in e.OldItems)
(SelectedItems as IList).Remove(item);

_collectionChangedSuspended = false;
}

}

将此行为与 RadGridViews 一起使用,如下所示:
<i:Interaction.Behaviors>
<behaviors:SelectedSyncBehavior SelectedItems="{Binding ViewModel.SelectedItems}" />
</i:Interaction.Behaviors>

关于mvvm - 使用 MVVM 模式在 Telerik RadGridView 中查找选定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6525693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com