如何从代码后面选择多个单元格 [英] How to select multiple cells from code behind

查看:107
本文介绍了如何从代码后面选择多个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从后面的代码中选择数据网格的多个单元格。我对该函数的输入是 List< Tuple< int,int>> ,其中包含我想以行,列格式突出显示的所有单元格的位置。



基本上,我想以这样的方式突出显示它,以便取消选择任何先前选择的单元格并选择输入列表中的单元格。



如果 SelectedCellsChanged 事件被触发,那也不错。我也不在乎它是否多次开火。



我无法找到任何这样的例子



请帮忙。



--------------------从您的评论中添加您的代码---- ---



基本上我知道如何改变细胞的背景颜色。我可以使用这种方法得到单元格(已经使用了这个),你可以忽略突出标记的东西,因为它特定于我的应用程序。



I want to select multiple cells of a datagrid from code behind. My input to the function is a List<Tuple<int,int>> which contains the positions of all the cells I want to highlight in row,column format.

Basically, I want to highlight it in such a way such that any previously selected cells are unselected and the cells in the input list are selected.

It would also be nice if the SelectedCellsChanged event is fired. I dont care if it fired multiple times also.

I am unable to find any examples of this

Please help.

-------------------- Added your code from your comment -------

basically i know how to change the background colour of the cell. I can get the cell using this method (already using this), you can ignore the highlight flag stuff as it is specific to my app.

public void ColourCell(int row, int column, Color color, bool HighlightFlag = true)
{
     DataGridRow rowContainer = GetRow(row);

     if (rowContainer != null)
     {
            DataGridCellsPresenter presenter = GetVisualChild(rowContainer);
        // try to get the cell but it may possibly be virtualized but enable virtualizion is off
            DataGridCell cell = 
                   (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            // now try to bring into view and retreive the cell
            if (HighlightFlag == false)
                    cell.Background = new SolidColorBrush(Colors.MediumPurple);
            else
            {
                    cell.Background = new SolidColorBrush(color);
            }
      }
}







public DataGridRow GetRow(int index)
{
      DataGridRow row = 
            (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
      if (row == null)
      {
            row = (DataGridRow)DataGridControl.ItemContainerGenerator.ContainerFromIndex(index);
      }
      return row;
}







static T GetVisualChild(Visual parent) where T : Visual
{
      T child = default(T);
      int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
      for (int i = 0; i < numVisuals; i++)
      {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                  child = GetVisualChild(v);
            }
            if (child != null)
            {
                    break;
            }
      }
      return child;
}

推荐答案

如果你还没有读过这个:

http://msdn.microsoft.com/en-us/library/system。 windows.controls.datagrid.selectedcells.aspx [ ^ ]



http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(v=vs.100 ).aspx [ ^ ]

确保数据网格上的选择单位设置为单元格。



我会查看单元格对象上的IsSelected属性。如果在开始时清除所有选定的单元格,则在对特定单元格执行突出显示时,将IsSelected选项设置为true。
Have a read of this if you have not already:
http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.selectedcells.aspx[^]
and
http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(v=vs.100).aspx[^]
Make sure your selection unit on the datagrid is set to Cell.

I would look at the "IsSelected" property on the cell objects. If you clear all selected cells at the start then as you perform your highlighting on specific cells set the "IsSelected" option to true.


这篇关于如何从代码后面选择多个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆