栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从MVVM WPF项目的DataGrid中选择多个项目

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

我的

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

我的模特:

public class MyModel{    public string Name { get; set; }    public int Age { get; set; }}

最后,这是后面的代码

MainWindow

public partial class MainWindow : Window{    public MainWindow ()    {        InitializeComponent ();        this.DataContext = new MyViewModel ();    }}

我希望这种干净的MVVM设计能有所帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/568472.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号