2011-06-29 68 views
3

我在我的應用程序中使用了Telerik RadGridView,它有一個GridViewSelectColumn項目,它允許我選擇網格中的各種項目。我有一個按鈕可以操作這個選項,但不知道如何獲取選定項目的列表。問題是我正在使用Caliburn.Micro的MVVM模式。我是否需要在視圖中找到控件並遍歷所選項目的列表?對於一項簡單的任務來說,這似乎很多工作。我會很感激任何想法。使用MVVM模式在Telerik RadGridView中查找選定的項目

回答

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); 
      } 
     } 

sample code - bitbucket

download

+0

我知道這是一段時間,但我試圖讓這個工作。您能否展示XAML?因爲我似乎無法獲得綁定的工作。 另外,你可以使源代碼可以下載,而無需通過bitbucket,因爲我無法在那裏得到它。 – Dave

+0

@Dave添加了下載的回購 –

+0

謝謝德里克。我似乎誤解了一些東西。我看到你在Customer上有一個IsSelected屬性,但我在XAML中找不到綁定。 – Dave

-1

有一個情況下,你不能例如添加布爾(ObseravbleCollection

請看this解決方案。

+0

有人有這個博客文章工作?我放棄了它,RadGridView甚至沒有SelectedItems屬性... – GP24

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; 
    } 
} 
相關問題