我在我的應用程序中使用了Telerik RadGridView,它有一個GridViewSelectColumn項目,它允許我選擇網格中的各種項目。我有一個按鈕可以操作這個選項,但不知道如何獲取選定項目的列表。問題是我正在使用Caliburn.Micro的MVVM模式。我是否需要在視圖中找到控件並遍歷所選項目的列表?對於一項簡單的任務來說,這似乎很多工作。我會很感激任何想法。使用MVVM模式在Telerik RadGridView中查找選定的項目
3
A
回答
1
添加布爾IsSelected到該項目在您的收藏:
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsSelected { get; set; }
}
private BindableCollection<Customer> _customers;
public BindableCollection<Customer> Customers
{
get { return _customers; }
set
{
_customers = value;
NotifyOfPropertyChange(() => Customers);
}
}
-1
10
Telerik的RadGridView的問題是,它的SelectedItem集合是隻讀的,所以你不能將雙向綁定到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>
+3
這應該是選定的答案! telerik博客似乎都缺乏這裏找到的完整解決方案。 – Michael
+1
不要忘記實例化SelectedItems屬性綁定的集合,浪費15分鐘... –
0
這裏是@ Knasterbax與明確的私人改性劑和空傳播類清理副本:
// Behavior for synchronizing Telerik RadDataGrid's SelectedItems collection
// with a SelectedItems collection of the ViewModel.
public class SelectedSyncBehavior : Behavior<RadGridView>
{
private bool collectionChangedSuspended;
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems",
typeof(INotifyCollectionChanged), typeof(SelectedSyncBehavior), new PropertyMetadata(OnSelectedItemsPropertyChanged));
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged;
}
public INotifyCollectionChanged SelectedItems
{
get { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
var collection = args.NewValue as INotifyCollectionChanged;
if (collection == null) return;
var selectedSyncBehavior = target as SelectedSyncBehavior;
if (selectedSyncBehavior != null) collection.CollectionChanged += selectedSyncBehavior.ContextSelectedItems_CollectionChanged;
}
private void ContextSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (collectionChangedSuspended) return; // Don't react recursively to CollectionChanged events
collectionChangedSuspended = true;
if (e.NewItems != null)
foreach (var item in e.NewItems)
AssociatedObject.SelectedItems.Add(item);
if (e.OldItems != null)
foreach (var item in e.OldItems)
AssociatedObject.SelectedItems.Remove(item);
collectionChangedSuspended = false;
}
private void GridSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (collectionChangedSuspended) return; // Don't react recursively to CollectionChanged events
collectionChangedSuspended = true;
if (e.NewItems != null)
foreach (var item in e.NewItems)
{
var list = SelectedItems as IList;
list?.Add(item);
}
if (e.OldItems != null)
foreach (var item in e.OldItems)
{
var list = SelectedItems as IList;
list?.Remove(item);
}
collectionChangedSuspended = false;
}
}
0
下面是上述答案的清理版本。這刪除下劃線,添加限定符,訪問器,大括號等。
public class SelectedItemsBehavior : Behavior<RadGridView>
{
private bool collectionChangedSuspended;
/// <summary>
/// Called after the behavior is attached to an AssociatedObject.
/// </summary>
/// <remarks>
/// Override this to hook up functionality to the AssociatedObject.
/// </remarks>
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.SelectedItems.CollectionChanged += this.GridSelectedItemsCollectionChanged;
}
/// <summary>
/// Getter/Setter for DependencyProperty, bound to the DataContext's SelectedItems ObservableCollection
/// </summary>
public INotifyCollectionChanged SelectedItems
{
get => (INotifyCollectionChanged)this.GetValue(SelectedItemsProperty);
set => this.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(SelectedItemsBehavior), 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)
{
collection.CollectionChanged += ((SelectedItemsBehavior)target).ContextSelectedItemsCollectionChanged;
}
}
/// <summary>
/// Will be called, when the Network's SelectedItems collection changes
/// </summary>
private void ContextSelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (this.collectionChangedSuspended)
{
return;
}
this.collectionChangedSuspended = true;
if (e.NewItems != null)
{
foreach (object item in e.NewItems)
{
this.AssociatedObject.SelectedItems.Add(item);
}
}
if (e.OldItems != null)
{
foreach (object item in e.OldItems)
{
this.AssociatedObject.SelectedItems.Remove(item);
}
}
this.collectionChangedSuspended = false;
}
/// <summary>
/// Will be called when the GridView's SelectedItems collection changes
/// </summary>
private void GridSelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (this.collectionChangedSuspended)
{
return;
}
this.collectionChangedSuspended = true;
if (e.NewItems != null)
{
foreach (object item in e.NewItems)
{
((IList)this.SelectedItems).Add(item);
}
}
if (e.OldItems != null)
{
foreach (object item in e.OldItems)
{
((IList)this.SelectedItems).Remove(item);
}
}
this.collectionChangedSuspended = false;
}
}
相關問題
- 1. 使用MVVM模式實現Telerik VirtualQueryableCollectionView
- 2. 如何使用MVVM模式在LongListSelector中選擇一個項目?
- 3. Silverlight Telerik RadCombobox在RadGridView綁定問題中
- 4. 列表框在WPF中使用MVVM中的選定項目
- 5. Telerik Grid - 引用選定的項目
- 6. Telerik RadGridView檢查Cell是否處於編輯模式
- 7. Telerik RadGridView問題
- 8. MVVM Treeview選定項目
- 9. C#Telerik的RadGridview CellBeginEdit
- 10. Telerik的RadGridView分組
- 11. 從Telerik RadGridView中的選定行中獲取隱藏列值
- 12. TypeAccessException使用RIA服務和Telerik的RadGridView
- 13. 使用BeautifulSoup查找選定的選項
- 14. 使用MVVM模式
- 15. Telerik/radgrid等效(查找行項目/值)?
- 16. Telerik radgridview合併/組
- 17. telerik winforms linq to radgridview
- 18. wintid在telerik radgridview中的組合框
- 19. Telerik GridViewPinButton在RadGridView中的可見性
- 20. WPF Telerik DataFormCombobbox在編輯模式下丟失了其選定的項目
- 21. 數據綁定後的Telerik RadGridView事件
- 22. 首先點擊Telerik RadGridView激活RadGridview,但不選擇一行
- 23. 動態使用RX和MVVM模式將項目放入Spinner中?
- 24. 選擇使用WPF從checkedlistbox項目,MVVM
- 25. 在XAML的telerik網格中禁用特定列使用MVVM
- 26. 高頻刷新Telerik的radgridview
- 27. Telerik radgridview時間戳自動格式
- 28. Telerik RadGridView樣式列單元格
- 29. 查找選定的Ultralistview項目
- 30. 在Asp.net ListView中查找控件在選定的項目模板中
我知道這是一段時間,但我試圖讓這個工作。您能否展示XAML?因爲我似乎無法獲得綁定的工作。 另外,你可以使源代碼可以下載,而無需通過bitbucket,因爲我無法在那裏得到它。 – Dave
@Dave添加了下載的回購 –
謝謝德里克。我似乎誤解了一些東西。我看到你在Customer上有一個IsSelected屬性,但我在XAML中找不到綁定。 – Dave