如何在数据网格上绘制线条

时间:2014-08-14 20:42:49

标签: c# wpf

如何在数据网格上绘制一条线,我正在尝试这样但是不起作用。 “row”变量始终为null Falor ......

    private void PersonData()
    {
        foreach (SingleEstoqueEntrada item in DataGridEstoque.ItemsSource)
        {
            try
            {
                var row = DataGridEstoque.ItemContainerGenerator.ContainerFromIndex(I) as DataGridRow;
                if (item.ESTOQUE == 0.00m)
                    row.Background = Brushes.Pink;
                else
                    row.Background = Brushes.YellowGreen;
            }
            catch (Exception _Exception)
            {
                MessageBox.Show(_Exception.Message);
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

如果数据被正确绑定并且提到了属性ESTOQUE(也从代码中假设),将此样式放在资源中可能会产生预期的效果

    <Style TargetType="DataGridRow">
        <Setter Property="Background"
                Value="YellowGreen" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding ESTOQUE}"
                         Value="0">
                <Setter Property="Background"
                        Value="Pink" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

示例xaml

    <DataGrid>
        <DataGrid.Resources>
            <Style TargetType="DataGridRow">
                <Setter Property="Background"
                        Value="YellowGreen" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding}"
                                 Value="item 1">
                        <Setter Property="Background"
                                Value="Pink" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding}"
                                Header="A Column" />
            <DataGridTextColumn Binding="{Binding Length}"
                                Header="Length" />
        </DataGrid.Columns>
        <sys:String>item 1</sys:String>
        <sys:String>item 2</sys:String>
        <sys:String>item 1</sys:String>
        <sys:String>item 2</sys:String>
        <sys:String>item 2</sys:String>
        <sys:String>item 2</sys:String>
    </DataGrid>

结果

result

在上面的示例中,我编写了触发器来修改所有具有item 1

的行

答案 1 :(得分:0)

尝试

var row = DataGridEstoque.ItemContainerGenerator.ContainerFromItem(item)as DataGridRow;

而不是

var row = DataGridEstoque.ItemContainerGenerator.ContainerFromIndex(I) as DataGridRow;