无法找到Datagrid Wpf的行

时间:2016-05-18 14:17:04

标签: c# wpf datagrid

的Xaml:

<DataGrid 
    Name="dgIssuedItemsBatchWise" 
    AutoGenerateColumns="False" 
    CanUserAddRows="False" 
    Loaded="dgIssuedItemsBatchWise_Loaded" 
    LoadingRow="dgIssuedItemsBatchWise_LoadingRow" 
    LoadingRowDetails="dgIssuedItemsBatchWise_LoadingRowDetails"
    >
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
        </Style>
    </DataGrid.CellStyle>
    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <Expander 
                Expanded="Expander_Expanded" 
                Collapsed="Expander_Collapsed">
            </Expander>
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>
    <DataGrid.Columns>
        <DataGridCheckBoxColumn 
            Width="100" 
            Binding="{Binding IsIndentIssue,UpdateSourceTrigger=PropertyChanged}"
            >
            <DataGridCheckBoxColumn.Header  >
                <TextBlock 
                    FontWeight="Bold" 
                    Text="Is Selected"
                    />
            </DataGridCheckBoxColumn.Header>
        </DataGridCheckBoxColumn>
        <!--<DataGridCheckBoxColumn Width="100"  Header="Is Selected"   Binding="{Binding IsIndentIssue}" />-->
        <DataGridTextColumn 
            Width="150" 
            IsReadOnly="True" 
            Header="Item Name" 
            FontWeight="Bold"  
            Binding="{Binding ItemName}" 
            />
        <DataGridTextColumn 
            Width="100" 
            IsReadOnly="True" 
            Header="Item Code" 
            FontWeight="Bold"  
            Binding="{Binding ItemCode}" 
            />
        <DataGridTextColumn 
            Width="100" 
            IsReadOnly="True" 
            x:Name="reqQuantity"  
            Header="Required Quantity" 
            FontWeight="Bold"   
            Binding="{Binding Quantity}" />
        <DataGridTextColumn 
            Width="100" 
            IsReadOnly="True" 
            x:Name="issQty" 
            Header="Issued Quantity" 
            FontWeight="Bold"  
            Binding="{Binding IssueQuantity}" 
            />
    </DataGrid.Columns>
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" ></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                </Grid.RowDefinitions>
                <DataGrid Grid.Row="0"  
                    Height="Auto" 
                    Padding="10,10,10,10" 
                    Width="1320"  
                    Grid.ColumnSpan="5" 
                    ItemsSource="{Binding BatchWiseItems,UpdateSourceTrigger=PropertyChanged}" 
                    x:Name="dgIssuedItemsBatchDetails"  
                    AutoGenerateColumns="False"  
                    HorizontalAlignment="Stretch" 
                    VerticalAlignment="Center" 
                    CanUserAddRows="False"  
                    >
                    <DataGrid.Columns>
                        <!--<DataGridTextColumn Width="440"  >
                            <DataGridTextColumn.Header>
                                <CheckBox 
                                    HorizontalAlignment="Center" 
                                    VerticalAlignment="Center"  />
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>-->

                        <DataGridCheckBoxColumn 
                            Width="100" 
                            Binding="{Binding IsItemIssued,UpdateSourceTrigger=PropertyChanged}">
                            <DataGridCheckBoxColumn.Header  >
                                <TextBlock 
                                    FontWeight="Bold" 
                                    Text="Is Issued"
                                    />
                            </DataGridCheckBoxColumn.Header>
                        </DataGridCheckBoxColumn>

                        <!--<DataGridCheckBoxColumn Width="100"  Header="Is Issued" Binding="{Binding IsIndentIssue}"  />-->

                        <DataGridTextColumn 
                            Binding="{Binding BatchNo}" 
                            Width="440" >
                            <DataGridTextColumn.Header>
                                <TextBlock 
                                    FontWeight="Bold" 
                                    Text="Batch No"
                                    />
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn 
                            Binding="{Binding Quantity}" 
                            Width="440" >
                            <DataGridTextColumn.Header>
                                <TextBlock 
                                    FontWeight="Bold" 
                                    Text="Quantity"
                                    />
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>

                <!--<TextBox Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" ></TextBox>-->
            </Grid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>

我需要根据一些计算更改行颜色。在GridView中我们有RowDataBound个事件但在DataGrid中有人建议我使用DataGrid_LoadingRow事件。我使用该事件,如

private void dgIssuedItemsBatchWise_LoadingRow(object sender, DataGridRowEventArgs e)
{
    DataRowView item = e.Row.Item as DataRowView ;
    if (item != null)
    {
    }
}

但每次我将item作为null。有人建议我如何解决该问题或更改WPF DataGrid中的行颜色。

1 个答案:

答案 0 :(得分:2)

 private void dgIssuedItemsBatchWise_LoadingRow(object sender,
 DataGridRowEventArgs e) 
 { 
    DataRowView item = e.Row.DataContext as DataRowView ; 
    if (item != null) 
    {

    } 
 }

改变

 DataRowView item = e.Row.DataContext as DataRowView ; 

而不是

DataRowView item = e.Row.Item as DataRowView ; 

希望对你有帮助