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

发布于 2024-09-03 04:41:09 字数 149 浏览 5 评论 0原文

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

I have a WPFToolkit-datagrid and a View-model class with a property - "SelectedGridItems"
How do I bind selectedItem property of WPFToolkit-Datagrid to my viewmodel property ("SelectedGridItems") ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白芷 2024-09-10 04:41:09

这是一个附加属性,它挂接到 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。

Here is an attached property that hooks to the SelectionChanged event of the ListBox and populates a list of selected items to the ViewModel. You can simply change "ListBox" to "DataGrid".

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

Edit: Adding source code for DataGrid version...

/// <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
}

In the XAML:

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

"MyCollection" should be an ObservableCollection in your ViewModel.

绻影浮沉 2024-09-10 04:41:09

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

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

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

</DataGrid>

要绑定多个选定的行,最好的选择可能是创建一个附加属性,该属性维护选定项目的集合,然后可以将其数据绑定到视图模型。

编辑 - 下面添加了链接

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

This is code for WPF4's DataGrid, I don't have access to Toolkit at the moment, but I doubt it is different.

For binding a single row in a datagrid:

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

</DataGrid>

To bind multiple selected rows, your best bet is probably to create an attached property which maintains a collection of selected items which can then be data bound to viewmodel.

EDIT -- added link below

See here for article about creating an attached property doing very similar to what you want:
http://alexshed.spaces.live.com/Blog/cns!71C72270309CE838!149.entry

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文