从MVVM WPF项目中的DataGrid中选择多个项目 [英] Select multiple items from a DataGrid in an MVVM WPF project
本文介绍了从MVVM WPF项目中的DataGrid中选择多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从MVVM WPF项目中的 DataGrid
中选择多个项目?
解决方案
您可以简单地添加自定义依赖属性来执行此操作:
public class CustomDataGrid:DataGrid
{
public CustomDataGrid()
{
this.SelectionChanged + = CustomDataGrid_SelectionChanged;
}
void CustomDataGrid_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
this.SelectedItemsList = this.SelectedItems;
}
#region SelectedItemsList
public IList SelectedItemsList
{
get {return(IList)GetValue(SelectedItemsListProperty); }
set {SetValue(SelectedItemsListProperty,value); }
$ b public static readonly DependencyProperty SelectedItemsListProperty =
DependencyProperty.Register(SelectedItemsList,typeof(IList),typeof(CustomDataGrid),new PropertyMetadata(null));
#endregion
}
现在可以使用这个 dataGrid
在XAML中:
< Window x:Class =DataGridTesting .MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006 / xaml
xmlns:i =http://schemas.microsoft.com/expression/2010/interactivity
xmlns:local =clr-namespace:DataGridTesting.CustomDatagrid
标题=MainWindow
Height =350
宽度=525>
& DockPanel>
< local:CustomDataGrid ItemsSource ={Binding Model}
SelectionMode =扩展
AlternatingRowBackground =Aquamarine
SelectionUnit =FullRow
IsReadOnly = True
SnapsToDevicePixels =True
SelectedItemsList ={Binding TestSelected,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}/>
< / DockPanel>
< / Window>
我的 ViewModel
:
public class MyViewModel:INotifyPropertyChanged
{
private static object _lock = new object();
私人列表< MyModel> _myModel;
public IEnumerable< MyModel>模型{get {return _myModel; }}
private IList _selectedModels = new ArrayList();
public IList TestSelected
{
get {return _selectedModels; }
set
{
_selectedModels = value;
RaisePropertyChanged(TestSelected);
}
}
public MyViewModel()
{
_myModel = new List< MyModel> ();
BindingOperations.EnableCollectionSynchronization(_myModel,_lock); (int i = 0; i< 10; i ++)
{
_myModel.Add(new MyModel
{
Name =Test + i,
Age = i * 22
});
}
RaisePropertyChanged(Model);
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
var pc = PropertyChanged;
if(pc!= null)
pc(this,new PropertyChangedEventArgs(propertyName));
}
}
我的模型:
public class MyModel
{
public string Name {get;组; }
public int Age {get;组; }
}
最后,这里是 MainWindow
:
public partial class MainWindow:Window
{
public MainWindow ()
{
InitializeComponent();
this.DataContext = new MyViewModel();
}
}
我希望这个干净的MVVM设计有所帮助。 >
How can I select multiple items from a DataGrid
in an MVVM WPF project?
解决方案
You can simply add a custom dependency property to do this:
public class CustomDataGrid : DataGrid
{
public CustomDataGrid ()
{
this.SelectionChanged += CustomDataGrid_SelectionChanged;
}
void CustomDataGrid_SelectionChanged (object sender, SelectionChangedEventArgs e)
{
this.SelectedItemsList = this.SelectedItems;
}
#region SelectedItemsList
public IList SelectedItemsList
{
get { return (IList)GetValue (SelectedItemsListProperty); }
set { SetValue (SelectedItemsListProperty, value); }
}
public static readonly DependencyProperty SelectedItemsListProperty =
DependencyProperty.Register ("SelectedItemsList", typeof (IList), typeof (CustomDataGrid), new PropertyMetadata (null));
#endregion
}
Now you can use this dataGrid
in the XAML:
<Window x:Class="DataGridTesting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:DataGridTesting.CustomDatagrid"
Title="MainWindow"
Height="350"
Width="525">
<DockPanel>
<local:CustomDataGrid ItemsSource="{Binding Model}"
SelectionMode="Extended"
AlternatingRowBackground="Aquamarine"
SelectionUnit="FullRow"
IsReadOnly="True"
SnapsToDevicePixels="True"
SelectedItemsList="{Binding TestSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DockPanel>
</Window>
My ViewModel
:
public class MyViewModel : INotifyPropertyChanged
{
private static object _lock = new object ();
private List<MyModel> _myModel;
public IEnumerable<MyModel> Model { get { return _myModel; } }
private IList _selectedModels = new ArrayList ();
public IList TestSelected
{
get { return _selectedModels; }
set
{
_selectedModels = value;
RaisePropertyChanged ("TestSelected");
}
}
public MyViewModel ()
{
_myModel = new List<MyModel> ();
BindingOperations.EnableCollectionSynchronization (_myModel, _lock);
for (int i = 0; i < 10; i++)
{
_myModel.Add (new MyModel
{
Name = "Test " + i,
Age = i * 22
});
}
RaisePropertyChanged ("Model");
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged (string propertyName)
{
var pc = PropertyChanged;
if (pc != null)
pc (this, new PropertyChangedEventArgs (propertyName));
}
}
My model:
public class MyModel
{
public string Name { get; set; }
public int Age { get; set; }
}
And finally, here is the code behind of MainWindow
:
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent ();
this.DataContext = new MyViewModel ();
}
}
I hope this clean MVVM design helps.
这篇关于从MVVM WPF项目中的DataGrid中选择多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文