迭代WPF数据网格中的行,然后相应地将行值传递给方法和样式行

时间:2012-11-13 14:15:38

标签: c# wpf datagrid styles

我有一个WPF数据网格,其数据源作为其来源。 datagrid包含一个包含rowID的不可见列。我需要做的是迭代datagird中的每一行,拉出rowID并将其传递给bool IsArchived( int rowID )方法。如果此方法返回true,我想以某种方式设置datagrid行的样式。

我尝试按照我发现here

的方式实施该方法
private void colorArchived( DataGrid grid , GX3MaterialSelectionData data)
        {
            var rows = GetDataGridRows(grid);

            foreach (DataGridRow row in rows)
            {
                DataRowView rv = (DataRowView)row.Item;
                int rowID = (int)rv.Row[1];
                data.IsArchived( rowID );

            }
        }

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

但是行var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;总是返回null。

我想以编程方式尽可能多地执行此操作。

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

1 个答案:

答案 0 :(得分:1)

这有点长,但我会使用样式选择器来完成这项工作,

我在以下窗口中设置了数据网格作为示例。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Window.Resources>
        <local:RowStyleSelector x:Key="styleSelector"/>
    </Window.Resources>
    <Grid>
        <DataGrid  ItemsSource="{Binding Items}" RowStyleSelector="{StaticResource styleSelector}"/>
    </Grid>
</Window>

样式选择器在下面的代码中定义,请注意TestClass表示您放入网格的对象。

RowStyleSelector类将为添加到网格中的每一行运行一次SelectStyle方法。

public partial class MainWindow : Window
{
    public ObservableCollection<TestClass> Items
    {
        get { return (ObservableCollection<TestClass>)GetValue(ItemsProperty); }
        set { SetValue(ItemsProperty, value); }
    }

    public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<TestClass>), typeof(MainWindow), new PropertyMetadata(null));

    public MainWindow()
    {
        InitializeComponent();
        Items = new ObservableCollection<TestClass>();
        for (int i = 0; i < 100; i++)
            Items.Add(new TestClass()
            {
                ID = i,
                Text = "Text for row " + i.ToString()
            });
    }
}

public class RowStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        TestClass targetIem = item as TestClass;
        if (targetIem != null)
        {
            // You can work with your data here.
            if (targetIem.ID == 0)
            {
                // Locate and return the style for when ID = 0.
                return (Style)Application.Current.FindResource("ResourceName");
            }
            else
                return base.SelectStyle(item, container);
        }
        else
            return base.SelectStyle(item, container);
    }
}

public class TestClass
{
    public int ID { get; set; }
    public string Text { get; set; }
}

编辑: 在回复下面的评论时,请参阅修订后的RowStyleConverter,您将不需要TestClass。

public class RowStyleSelector : StyleSelector
{
    public override Style SelectStyle(object item, DependencyObject container)
    {
        System.Data.DataRow targetItem = item as System.Data.DataRow;
        if (targetItem != null)
        {
            // You can work with your data here.
            if ((int)targetItem["IDColumn"] == 0)
            {
                // Locate and return the style for when ID = 0.
                return (Style)Application.Current.FindResource("ResourceName");
            }
            else
                return base.SelectStyle(item, container);
        }
        else
            return base.SelectStyle(item, container);            
    }
}