问题描述
我有一个WPFToolkit-datagrid和一个具有属性的View-model类 - 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") ?
推荐答案
这是一个附加属性,它挂接到ListBox的SelectionChanged事件,并将所选项目列表填充到ViewModel。您可以简单地将ListBox更改为DataGrid。
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".
编辑:添加DataGrid版本的源代码...
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
}
在XAML中:
<tk:DataGrid ... local:DataGridService.SelectedItems="{Binding Path=MyCollection}" SelectionMode="Extended">
MyCollection应该是ViewModel中的一个ObservableCollection。
"MyCollection" should be an ObservableCollection in your ViewModel.
这篇关于如何将WPFToolkit-Datagrid的selectedItem属性绑定到我的viewmodel属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!