从datarowview WPF获取datagridrow

时间:2012-11-20 09:58:35

标签: c# wpf datagrid

我试图循环遍历数据网格中的每一行,拉出一个列值,将此值传递给一个方法,并根据该方法的结果设置该行的样式。

在发现我无法循环遍历数据网格的行后,我发现this帖子详细说明了它是如何可能的。

我稍微修改了以便我正在使用datarowview对象。

我现在遇到的问题是

var dgRow = grid.ItemContainerGenerator.ContainerFromItem(r) as DataGridRow;

始终返回null。

请有人可以告诉我为什么会发生这种情况,如果他们能看到更简单的方法。

如果您需要更多信息,请与我们联系。

继承我的代码:

private void colorArchived( DataGrid grid , GX3MaterialSelectionData data)
    {
        var row = GetDataGridRows(grid);
        foreach (DataRowView r in row)
        {
            var dgRow = grid.ItemContainerGenerator.ContainerFromItem(r) as DataGridRow;
            int val = int.Parse(r.Row[0].ToString());
            if ( data.IsArchived(val) )
            {
                // style will be defined in xaml
                dgRow.Style = mystyle;
            }


        }

    }

    public IEnumerable<DataRowView> GetDataGridRows(DataGrid grid)
    {
        var itemsSource = grid.ItemsSource as IEnumerable;
        if (null == itemsSource) yield return null;
        foreach (var item in itemsSource)
        {
            var row = item;
            if (null != row) yield return (DataRowView)row;
        }
    }

3 个答案:

答案 0 :(得分:1)

根据您的问题,我刚刚更新了上述的StyleSelector类:

public class RowStyle : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        var dgRow = item as DataGridRow;
        int val = int.Parse(dgRow.Row[0].ToString());
        if ( data.IsArchived(val) )
        {
            return Mystyle;
        }
        return base.SelectStyle(item, container);
    }

    // style will be defined in xaml
    public Style Mystyle
    {
        get;
        set;
    }
}

注意:将“GX3MaterialSelectionData数据”描述为类的静态,以便上述类可以直接访问它。

答案 1 :(得分:0)

在这种情况下你可以使用StyleSelector。

public class RowStyle : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        // here the item property is the entity that the grid row is bound to.
        // check whatever values you want on it and locate a matching style with
        // find resource.

        // return a reference to the correct style here

        // or allow this to run if you want the default style.
        return base.SelectStyle(item, container);
    }
}

要在数据网格上使用它,您需要设置RowStyleSelector属性。

<Window x:Class="Rich.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Rich"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:RowStyle x:Key="styleSelector"/>
    </Window.Resources>
    <Grid>
        <DataGrid ItemsSource="{Binding Items}" RowStyleSelector="{StaticResource styleSelector}">            
            <DataGrid.Columns>
                <DataGridTextColumn Header="test" Binding="{Binding Test1}"/>
                <DataGridTextColumn Header="test2" Binding="{Binding Test2}"/>
                <DataGridTextColumn Header="test3" Binding="{Binding Test3}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

答案 2 :(得分:0)

方法1:

//simple way using SelectionChanged or RowEditEnding event without DataRowView
        DataRowView row = (DataRowView)t_DataGrid.SelectedItems[0];

        DataGridRow row1 = e.Row;

方法2:

    //convert  datagridrow to datarowview (it works sometimes only databinding problems)
    DataGridRow dgr = FindVisualParent<DataGridRow>(txtPcode);
    DataRowView drv = dgr.DataContext as DataRowView;

方法3:

//it needs improvements
DataRowView dataRow = (DataRowView)t_DataGrid.SelectedItem;
//subitem
int i_cell = t_DataGrid.CurrentCell.Column.DisplayIndex;
int i= int.Parse(dataRow.Row.ItemArray[id_cell].ToString());
//get datagrid by index
DataGridRow dgr = DG.GetRow(t_DataGrid_temp, i);

//create this special Datagrid class for getting full access over datagrid
public static class DG
{
    /* Get the Cell using Row Index and Column Index */
    public static DataGridCell GetCell(DataGrid grid, int row, int column)
    {
        DataGridRow rowContainer = GetRow(grid, row);
        return GetCell(grid, rowContainer, column);
    }

    /* Get the DataGridRow using Row Index */
    public static DataGridRow GetRow(DataGrid grid, int index)
    {
        DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // May be virtualized, bring into view and try again.
            grid.UpdateLayout();
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }

    /* Get the Cell using DataGridRow Object and Column Index */
    public static DataGridCell GetCell(DataGrid grid, DataGridRow row, int column)
    {
        if (row != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

            if (presenter == null)
            {
                grid.ScrollIntoView(row, grid.Columns[column]);
                presenter = GetVisualChild<DataGridCellsPresenter>(row);
            }

            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            return cell;
        }
        return null;
    }

    /* Get the Child Element Based on Type */
    public 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;
    }

    /* Get the Child by name */
    public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
    {
        if (parent == null) return null;

        T foundChild = null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            T childType = child as T;
            if (childType == null)
            {
                foundChild = FindChild<T>(child, childName);
                if (foundChild != null) break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                foundChild = (T)child;
                break;
            }
        }
        return foundChild;
    }
    /*get parent*/
    public static T GetParent<T>(DependencyObject d) where T : class
    {
        while (d != null && !(d is T))
        {
            d = VisualTreeHelper.GetParent(d);
        }
        return d as T;
    }

    //new
    public static Visual GetChildrenByType(Visual visualElement, Type typeElement, string nameElement)
    {
        if (visualElement == null) return null;
        if (visualElement.GetType() == typeElement)
        {
            FrameworkElement fe = visualElement as FrameworkElement;
            if (fe != null)
            {
                if (fe.Name == nameElement)
                {
                    return fe;
                }
            }
        }
        Visual foundElement = null;
        if (visualElement is FrameworkElement)
            (visualElement as FrameworkElement).ApplyTemplate();
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visualElement); i++)
        {
            Visual visual = VisualTreeHelper.GetChild(visualElement, i) as Visual;
            foundElement = GetChildrenByType(visual, typeElement, nameElement);
            if (foundElement != null)
                break;
        }
        return foundElement;
    }
}