首先增加一个附加属性类

DataGridSelectedCellsBehavior

    public class DataGridSelectedCellsBehavior{public static IList<DataGridCellInfo> GetSelectedCells(DependencyObject obj){return (IList<DataGridCellInfo>)obj.GetValue(SelectedCellsProperty);}public static void SetSelectedCells(DependencyObject obj, IList<DataGridCellInfo> value){obj.SetValue(SelectedCellsProperty, value);}public static readonly DependencyProperty SelectedCellsProperty = DependencyProperty.RegisterAttached("SelectedCells", typeof(IList<DataGridCellInfo>), typeof(DataGridSelectedCellsBehavior), new UIPropertyMetadata(null, OnSelectedCellsChanged));static SelectedCellsChangedEventHandler GetSelectionChangedHandler(DependencyObject obj){return (SelectedCellsChangedEventHandler)obj.GetValue(SelectionChangedHandlerProperty);}static void SetSelectionChangedHandler(DependencyObject obj, SelectedCellsChangedEventHandler value){obj.SetValue(SelectionChangedHandlerProperty, value);}static readonly DependencyProperty SelectionChangedHandlerProperty = DependencyProperty.RegisterAttached("SelectedCellsChangedEventHandler", typeof(SelectedCellsChangedEventHandler), typeof(DataGridSelectedCellsBehavior), new UIPropertyMetadata(null));//d is MultiSelector (d as ListBox not supported)static void OnSelectedCellsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args){if (GetSelectionChangedHandler(d) != null)return;if (d is DataGrid)//DataGrid{DataGrid datagrid = d as DataGrid;SelectedCellsChangedEventHandler selectionchanged = null;foreach (var selected in GetSelectedCells(d) as IList<DataGridCellInfo>)datagrid.SelectedCells.Add(selected);selectionchanged = (sender, e) =>{SetSelectedCells(d, datagrid.SelectedCells);};SetSelectionChangedHandler(d, selectionchanged);datagrid.SelectedCellsChanged += GetSelectionChangedHandler(d);}}}

界面

Help:DataGridSelectedCellsBehavior.SelectedCells="{Binding Path=SelectedGridCellCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

   <DataGrid  x:Name="WeekDataGrid" ItemsSource="{Binding Data}" ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"   HeadersVisibility="Column" CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False" IsReadOnly="True" CanUserResizeColumns="False" CanUserReorderColumns="False"Background="White" GridLinesVisibility="All"  AlternationCount="4"  SelectionUnit="Cell" SelectionMode="Extended" Margin="0" Grid.Column="1" CellStyle="{DynamicResource DataGridCellStyle}"Help:DataGridSelectedCellsBehavior.SelectedCells="{Binding Path=SelectedGridCellCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"><DataGrid.Columns><DataGridTemplateColumn Width="215" Visibility="Hidden" ><DataGridTemplateColumn.CellTemplate><DataTemplate><TextBlock Height="30"  Text="{Binding ID}" VerticalAlignment="Stretch"
HorizontalAlignment="Center"   /></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="215" ><DataGridTemplateColumn.HeaderTemplate><DataTemplate><Grid Height="63"  Width="215"   ><TextBlock Text=" " HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid></DataTemplate></DataGridTemplateColumn.HeaderTemplate><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid Height="30"   Background="#00000000" ><local:DataTableItem Height="30"   Text="{Binding Sun}" VerticalAlignment="Center"></local:DataTableItem></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="215" ><!--<DataGridTemplateColumn.HeaderTemplate><DataTemplate><Grid Height="60"  Width="215"><TextBlock Text="周一" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid></DataTemplate></DataGridTemplateColumn.HeaderTemplate>--><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid Height="30"    Background="#00000000" ><local:DataTableItem x:Name="ItemMon" Height="30" Text="{Binding Mon}" VerticalAlignment="Center"><i:Interaction.Triggers><i:EventTrigger EventName="MouseRightButtonDown"><i:InvokeCommandAction Command="{Binding  DataContext.DataGridCellMouseRightClick,ElementName=WeekTimeControl}" CommandParameter="{Binding ElementName=ItemMon}"></i:InvokeCommandAction></i:EventTrigger></i:Interaction.Triggers></local:DataTableItem></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="215" ><!--<DataGridTemplateColumn.HeaderTemplate><DataTemplate><Grid Height="60"><TextBlock Text="周二" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid></DataTemplate></DataGridTemplateColumn.HeaderTemplate>--><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid Height="30"    Background="#00000000" ><local:DataTableItem Height="30" x:Name="ItemTue"  Text="{Binding Tue}" VerticalAlignment="Center"><i:Interaction.Triggers><i:EventTrigger EventName="MouseRightButtonDown"><i:InvokeCommandAction Command="{Binding  DataContext.DataGridCellMouseRightClick,ElementName=WeekTimeControl}" CommandParameter="{Binding ElementName=ItemTue}"></i:InvokeCommandAction></i:EventTrigger></i:Interaction.Triggers></local:DataTableItem></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="215" ><!--<DataGridTemplateColumn.HeaderTemplate><DataTemplate><Grid Height="60"><TextBlock Text="周三" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid></DataTemplate></DataGridTemplateColumn.HeaderTemplate>--><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid Height="30"   Background="#00000000" ><local:DataTableItem Height="30" x:Name="ItemWed"  Text="{Binding Wed}" VerticalAlignment="Center" ><i:Interaction.Triggers><i:EventTrigger EventName="MouseRightButtonDown"><i:InvokeCommandAction Command="{Binding  DataContext.DataGridCellMouseRightClick,ElementName=WeekTimeControl}" CommandParameter="{Binding ElementName=ItemWed}"></i:InvokeCommandAction></i:EventTrigger></i:Interaction.Triggers></local:DataTableItem></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="215" ><!--<DataGridTemplateColumn.HeaderTemplate><DataTemplate><Grid Height="60"><TextBlock Text="周四" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid></DataTemplate></DataGridTemplateColumn.HeaderTemplate>--><DataGridTemplateColumn.CellTemplate><DataTemplate ><Grid Height="30"    Background="#00000000" ><local:DataTableItem Height="30" x:Name="ItemThu" Text="{Binding Thu}" VerticalAlignment="Center"><i:Interaction.Triggers><i:EventTrigger EventName="MouseRightButtonDown"><i:InvokeCommandAction Command="{Binding  DataContext.DataGridCellMouseRightClick,ElementName=WeekTimeControl}" CommandParameter="{Binding ElementName=ItemThu}"></i:InvokeCommandAction></i:EventTrigger></i:Interaction.Triggers></local:DataTableItem></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="215"   ><!--<DataGridTemplateColumn.HeaderTemplate><DataTemplate><Grid Height="60"><TextBlock Text="周五" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid></DataTemplate></DataGridTemplateColumn.HeaderTemplate>--><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid Height="30"   Background="#00000000" ><local:DataTableItem Height="30" x:Name="ItemFri"   Text="{Binding Fri}" VerticalAlignment="Center" HorizontalAlignment="Center"  ><i:Interaction.Triggers><i:EventTrigger EventName="MouseRightButtonDown"><i:InvokeCommandAction Command="{Binding  DataContext.DataGridCellMouseRightClick,ElementName=WeekTimeControl}" CommandParameter="{Binding ElementName=ItemFri}"></i:InvokeCommandAction></i:EventTrigger></i:Interaction.Triggers></local:DataTableItem></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="215" ><!--<DataGridTemplateColumn.HeaderTemplate><DataTemplate><Grid Height="60"><TextBlock Text="周六" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid></DataTemplate></DataGridTemplateColumn.HeaderTemplate>--><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid Height="30"    Background="#00000000" ><local:DataTableItem Height="30" x:Name="ItemSat" Text="{Binding Sat}" VerticalAlignment="Center" HorizontalAlignment="Center"><i:Interaction.Triggers><i:EventTrigger EventName="MouseRightButtonDown"><i:InvokeCommandAction Command="{Binding  DataContext.DataGridCellMouseRightClick,ElementName=WeekTimeControl}" CommandParameter="{Binding ElementName=ItemSat}"></i:InvokeCommandAction></i:EventTrigger></i:Interaction.Triggers></local:DataTableItem></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid>

ViewMode 绑定

private IList<DataGridCellInfo> selectedGridCellCollection = new List<DataGridCellInfo>();public IList<DataGridCellInfo> SelectedGridCellCollection{get { return selectedGridCellCollection; }set{selectedGridCellCollection = value;RaisePropertyChanged("SelectedGridCellCollection");}}

用过 SelectedGridCellCollection 就可以获得值了

WPF DataGrid MVVM 绑定 SelectedCells相关推荐

  1. (WPF) DataGrid之绑定

    通过ObservableCollection 绑定到 DataGrid. 1. 前台Xaml. <DataGrid x:Name="dgMeasurements"Horizo ...

  2. wpf之MVVM绑定背景色

    首先需要绑定一个参数 Background="{Binding bk,UpdateSourceTrigger=PropertyChanged}" 定义一个参数 SolidColor ...

  3. WPF DataGrid 和LINQ to SQL示例程序之一 (提供源代码下载)

    WPF DataGrid 和LINQ to SQL示例程序之一 (提供源代码下载) WPF DataGrid 系列示例程序,由浅入深逐步介绍如何在WPF 应用程序中使用新的DataGrid 控件.本篇 ...

  4. 使用绑定进行WPF DataGrid格式化的指南

    目录 介绍 WPF DataGrid结构 WPF绑定基础 使用的业务数据 将DataGrid与业务数据连接 DataGrid格式 格式化列 格式化完整行 根据显示的值格式化单元格 根据业务逻辑数据格式 ...

  5. WPF采用MVVM模式(绑定:纯前台、命令:触发器绑定命令)

    MVVM绑定 view-viewModel-model,模型介绍省略,就是创建类,添加字段封装属性.注:控件的绑定只能绑定到属性上,不能绑定到字段上: 接下来就是代码 (view): 1 <Wi ...

  6. wpf DataGrid主从表,DataGrid嵌套DataGrid主从结构rowdetailtemplate实现,绑定DataTable数据源,使用Visual Studio 2017

    wpf DataGrid主从表,DataGrid嵌套DataGrid主从结构rowdetailtemplate实现,绑定DataTable数据源,使用Visual Studio 2017 . 子表绑定 ...

  7. Wpf DataGrid 绑定Dynamic (二维数据表)

    最近有一个需求,需要根据用户数据动态生成二维数据表,绑定到表格后,用户再次编辑数据,最后再将编辑过的数据,生成类型数据,存入数据库. 仔细分析过后,觉得最适合的方式,莫过于给DataGrid绑定Dyn ...

  8. WPF DataGrid 如何将被选中行带到视野中

    WPF DataGrid 如何将被选中行带到视野中 目录 前言 准备工作 方法一 方法二 总结 独立观察员 2021 年 12 月 11 日 前言 在 WPF 开发中,显示表格一般使用 DataGri ...

  9. WPF DataGrid 通过自定义表头模拟首行固定

    WPF DataGrid 通过自定义表头模拟首行固定 独立观察员 2021 年 9 月 25 日 最近工作中要在 WPF 中做个表格,自然首选就是 DataGrid 控件了.问题是,UI 设计的表格是 ...

最新文章

  1. 【T10】记住,TCP__IP不是轮询的
  2. ES failed shard on node[XXX]: failed recovery, failure RecoveryFailedException XXX Too many openfile
  3. 第一次冲刺阶段(三)
  4. SpringBoot 异常回滚 事务的使用___Springboot @Transactional 事务不回滚
  5. Facebook 上传小游戏流程
  6. c语言的条件运算符,C语言条件运算符
  7. idea设置project路径_idea 启动界面修改显示的项目存储物理路径,reopen project
  8. 揭开 Java 注解的神秘面纱
  9. MAYA oceanShader/海洋(纹理)
  10. 步进电机五根线怎么接_软启动怎么接电机?软启动电机实物接线图
  11. 光武1号守卫盾,一款让优秀非凡的重疾产品【保通保险】
  12. Scaling Instruction-Finetuned Language Models翻译
  13. Spring-Boot启动方式,以及线上部署
  14. Kubernetes kata-container 介绍
  15. Linux —— 时间问题(GMT,UTC,DST,CST,CET表示什么、UTC与CST之间的转换)
  16. 7-9 7-10 sdut-C语言实验- 排序
  17. BIND的安装与配置
  18. 哈哈日语 五十音图中发音最难的四个假名
  19. 关于vue小插件:ly-tab的解释
  20. 江苏大学和南京工业大学计算机哪个好,江苏大学和浙江大学,哪个好?

热门文章

  1. 完整简洁的Oracle获得汉字字符串拼音首字母和全拼的函数
  2. 【ospf的三类LSA sum-net】(真假ABR、区域间防环机制、vlink)
  3. matlab regress bint,Matlab一个关于regress的困惑
  4. 温州科技职业学院 计算机网络技术,浙江【温州科技职业学院】_计算机网络技术专业建设方案.doc...
  5. 2021年上半年软件设计师考试评估解析
  6. 【面试题001】最强java八股文
  7. 拼多多显示服务器有点问题,拼多多为什么登录不上 登录不上解决方法
  8. 2022年CVPR挑战赛
  9. 打印表格留标题怎么设置_打印如何添加标题?内容一页打不下?—关于Excel文档打印的那些事-excel打印区域怎么设置...
  10. 我的世界(10)-快速建筑(创世神WorldEdit)