datagrid获取单元索引 [英] datagrid get cell index

查看:133
本文介绍了datagrid获取单元索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取单元格索引,其中列标题=column4,行包含232
,例如我上传的屏幕截图是否可以获得红色单元格索引,而不是使其变为红色?如果wpf datagrid有这个功能,wpf工具包数据网格有吗?列和行正在从

解决方案

您应该通过 Style / 触发器绑定与转换器如

 < DataGrid Name =dataGrid
...>
< DataGrid.Columns>
< DataGridTextColumn Header =column4Binding ={Binding column4}>
< DataGridTextColumn.CellStyle>
< Style TargetType =DataGridCell>
< Style.Triggers>
< DataTrigger Binding ={Binding column4}Value =232>
< Setter Property =BackgroundValue =Red/>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /DataGridTextColumn.CellStyle>
< / DataGridTextColumn>
<!--...-->
< /DataGrid.Columns>
<!--...-->
< / DataGrid>






默认情况下, DataGrid 正在使用虚拟化,所以目前用户只能看到 DataGridRows 。其他行将被创建一旦它们变得可见,所以如果你试图在代码背后的一些单元格可以变得相当凌乱(你想要访问的单元格甚至可能不存在)。

要在索引行/列中获取一个 DataGridCell ,您可以定义一个帮助类( DataGridHelper )并使用它像这样

  DataGridCell cell = DataGridHelper.GetCell(dataGrid,0,2); 
if(cell!= null)
{
cell.Background = Brushes.Red;
}

DataGridHelper

 静态类DataGridHelper 
{
static public DataGridCell GetCell(DataGrid dg,int row,int column)
{
DataGridRow rowContainer = GetRow(dg,row);

if(rowContainer!= null)
{
DataGridCellsPresenter Presenter = GetVisualChild< DataGridCellsPresenter>(rowContainer);

//尝试获取单元格但可能会被虚拟化
DataGridCell cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if(cell == null)
{
//现在尝试带入视图并检索单元格
dg.ScrollIntoView(rowContainer,dg.Columns [column]);
cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
返回单元格;
}
返回null;
}

static public DataGridRow GetRow(DataGrid dg,int index)
{
DataGridRow row =(DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
if(row == null)
{
//可能被虚拟化,带入视图并再次尝试
dg.ScrollIntoView(dg.Items [index]);
row =(DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
}
返回行;
}

static T GetVisualChild< T>(Visual parent)其中T:Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent); (int i = 0; i< numVisuals; i ++)
{
Visual v =(Visual)VisualTreeHelper.GetChild(parent,i);

child = v as T;
if(child == null)
{
child = GetVisualChild< T>(v);
}
if(child!= null)
{
break;
}
}
return child;
}
}


Does it possible to get cell index where column header = "column4" and row contains "232" for example i uploaded screen shot does it possible to get red cell index and than make it color red? and if wpf datagrid have that function does wpf toolkit data grid has? columns and rows are adding from code behind

解决方案

You should be doing this through Style/Trigger or Binding with a converter like

<DataGrid Name="dataGrid"
          ...>
    <DataGrid.Columns>
        <DataGridTextColumn Header="column4" Binding="{Binding column4}">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="DataGridCell">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding column4}" Value="232">
                            <Setter Property="Background" Value="Red"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
        <!--...-->
    </DataGrid.Columns>
    <!--...-->
</DataGrid>


By default, the DataGrid is using virtualization so only the DataGridRows that are visible to the user at the moment will be loaded. The other rows will be created once they become visible so if you're trying to style some cells in code behind in can become pretty messy (the cell you are trying to access might not even exist yet.)

To get a DataGridCell at index row/column you can define a helper class (DataGridHelper) and use it like this

DataGridCell cell = DataGridHelper.GetCell(dataGrid, 0, 2);
if (cell != null)
{
    cell.Background = Brushes.Red;
}

DataGridHelper

static class DataGridHelper
{
    static public DataGridCell GetCell(DataGrid dg, int row, int column)
    {
        DataGridRow rowContainer = GetRow(dg, row);

        if (rowContainer != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

            // try to get the cell but it may possibly be virtualized
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null)
            {
                // now try to bring into view and retreive the cell
                dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
        return null;
    }

    static public DataGridRow GetRow(DataGrid dg, int index)
    {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // may be virtualized, bring into view and try again
            dg.ScrollIntoView(dg.Items[index]);
            row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }

    static T GetVisualChild<T>(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<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }
}

这篇关于datagrid获取单元索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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