如何将WPFToolkit-Datagrid的selectedItem属性绑定到我的viewmodel属性?

时间:2010-05-26 10:17:14

标签: wpf datagrid viewmodel selecteditem

我有一个WPFToolkit-datagrid和一个带有属性的View-model类 - “SelectedGridItems” 如何将WPFToolkit-Datagrid的selectedItem属性绑定到我的viewmodel属性(“SelectedGridItems”)?

2 个答案:

答案 0 :(得分:3)

这是一个附加属性,它挂钩ListBox的SelectionChanged事件,并将所选项的列表填充到ViewModel。您只需将“ListBox”更改为“DataGrid”。

http://marlongrech.wordpress.com/2009/06/02/sync-multi-select-listbox-with-viewmodel/

编辑:添加DataGrid版本的源代码...

/// <summary>
/// Attached property that stores the selected items of a DataGrid
/// </summary>
public static class DataGridService
{
   #region SelectedItems

   /// <summary>
   /// SelectedItems Attached Dependency Property
   /// </summary>
   public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached(
       "SelectedItems", 
       typeof(IList), 
       typeof(DataGridService),
       new FrameworkPropertyMetadata((IList)null, new PropertyChangedCallback(OnSelectedItemsChanged)));

   /// <summary>
   /// Gets the SelectedItems property.
   /// </summary>
   /// <param name="d"><see cref="DependencyObject"/> to get the property from</param>
   /// <returns>The value of the SelectedItems property</returns>
   public static IList GetSelectedItems(DependencyObject d)
   {
      return (IList)d.GetValue(SelectedItemsProperty);
   }

   /// <summary>
   /// Sets the SelectedItems property.
   /// </summary>
   /// <param name="d"><see cref="DependencyObject"/> to set the property on</param>
   /// <param name="value">value of the property</param>
   public static void SetSelectedItems(DependencyObject d, IList value)
   {
      d.SetValue(SelectedItemsProperty, value);
   }

   /// <summary>
   /// Handles changes to the SelectedItems property.
   /// </summary>
   /// <param name="d"><see cref="DependencyObject"/> that fired the event</param>
   /// <param name="e">A <see cref="DependencyPropertyChangedEventArgs"/> that contains the event data.</param>
   private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
      var grid = (DataGrid)d;
      ReSetSelectedItems(grid);
      grid.SelectionChanged += delegate
      {
         ReSetSelectedItems(grid);
      };
   }

   /// <summary>
   /// Sets the selected items collection for the specified <see cref="DataGrid"/>
   /// </summary>
   /// <param name="grid"><see cref="DataGrid"/> to use for setting the selected items</param>
   private static void ReSetSelectedItems(DataGrid grid)
   {
      IList selectedItems = GetSelectedItems(grid);
      selectedItems.Clear();
      if (grid.SelectedItems != null)
      {
         foreach (var item in grid.SelectedItems)
         {
            selectedItems.Add(item);
         }
      }
   }

   #endregion
}

在XAML中:

<tk:DataGrid ... local:DataGridService.SelectedItems="{Binding Path=MyCollection}" SelectionMode="Extended">

“MyCollection”应该是ViewModel中的ObservableCollection。

答案 1 :(得分:1)

这是WPF4的DataGrid的代码,目前我无法访问Toolkit,但我怀疑它是不同的。

用于绑定数据网格中的单个行:

<DataGrid ItemsSource="{Binding ViewModelCollectionProperty}"
SelectedItem="{Binding Path=ViewModelProperty}">

</DataGrid>

要绑定多个选定的行,最好的办法是创建一个附加属性,该属性维护一组选定的项目,然后这些项目可以绑定到viewmodel。

编辑 - 添加了以下链接

有关创建与您想要的非常相似的附加属性的文章,请参阅此处: http://alexshed.spaces.live.com/Blog/cns!71C72270309CE838!149.entry