XAML 为什么在WPF中更改datagrid中某行或某列的值后,所需的数据没有显示?

tkqqtvp1  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(269)

我在C# WPF项目和DataGrid的Itemsource填充代码中有一个DataGrid。我为其中一列设置了默认值,但在双击该列之前,它不会显示该值!此外,在CellEndEdit中,我测试更改下一列的值,但在双击它之前,它无法真实的显示该值(单击以编辑它)

更改数据后我尝试了哪些操作:

DGR_SUB_INVOLST.Items.Refresh();
 DGR_SUB_INVOLST.BeginEdit();
 DGR_SUB_INVOLST.CommitEdit();
 DGR_SUB_INVOLST.InvalidateVisual();
 DGR_SUB_INVOLST.UpdateLayout();

我该怎么做

XAML文件:

<DataGrid x:Name="DGR_SUB_INVOLST"
                  EnableColumnVirtualization="True"
                  EnableRowVirtualization="True"
                  VirtualizingPanel.IsVirtualizing="True"
                  VirtualizingPanel.VirtualizationMode="Recycling"
                  ScrollViewer.CanContentScroll="False"
                  ItemsSource="{Binding ALL_DATA_INVO, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                   AutoGenerateColumns="False"  FlowDirection="RightToLeft" CellEditEnding="DGR_SUB_INVOLST_CellEditEnding" SelectionUnit="Cell">
            <DataGrid.Columns >
                <DataGridTextColumn Header="ردیف" x:Name="RADIF_COL"  Binding="{Binding RADIF}" MinWidth="40" Visibility="Hidden"/>

                <DataGridComboBoxColumn x:Name="ANBAR_COL" Width="80" Header=" انبار " SelectedValueBinding="{Binding CODE,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="NAMES" SelectedValuePath="CODE">
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="IsEditable" Value="True"/>
                            <Setter Property="ItemsPanel" Value="{StaticResource VSP}"/>
                            <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
                            <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
                            <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>

                <DataGridTextColumn Header="نام کالا" x:Name="NAME_COL" Binding="{Binding NAME,UpdateSourceTrigger=PropertyChanged}" Visibility="Visible" MinWidth="180"/>
                <DataGridTextColumn Header="کد کالا" x:Name="CODEKALA_COL" Binding="{Binding CODEKALA,UpdateSourceTrigger=PropertyChanged}" Visibility="Hidden" MinWidth="80"/>

            </DataGrid.Columns>
        </DataGrid>

C#:

private void DGR_SUB_INVOLST_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (DGR_SUB_INVOLST != null)
    {
        if (DGR_SUB_INVOLST.Items.Count > 0)
        {
            (e.Row.Item as INVOMonitor).CODE = 1;
            //Refresh here :
            DGR_SUB_INVOLST.Items.Refresh();
            DGR_SUB_INVOLST.BeginEdit();
            DGR_SUB_INVOLST.CommitEdit();
            DGR_SUB_INVOLST.InvalidateVisual();
            DGR_SUB_INVOLST.UpdateLayout();
        }
    }
}

结果enter image description here实际上项目行确实已更改,但未显示!

都不行!2请指导我

58wvjzkj

58wvjzkj1#

我认为您应该像这样修改Cell

1-正在获取ColumnRow索引:

private void DGR_SUB_INVOLST_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
    {
        if (DGR_SUB_INVOLST != null)
        {
            if (DGR_SUB_INVOLST.Items.Count > 0)
            {
                OldRowitem = (INVOMonitor)DGR_SUB_INVOLST.CurrentItem;
                DataGridColumn col1 = e.Column;
                DataGridRow row1 = e.Row;
                OldRowindex = ((DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
                OldColumnindex = col1.DisplayIndex;
            }
        }
    }

2-获取单元格的方法:

private DataGridCell GetCellVal(string NAME_SUTUN,int ROWINEX)
    {
        var TheCol = DGR_SUB_INVOLST.Columns.FirstOrDefault(c => c.SortMemberPath == NAME_SUTUN).DisplayIndex;
        var DGCInf = new DataGridCellInfo(DGR_SUB_INVOLST.Items[ROWINEX], DGR_SUB_INVOLST.Columns[TheCol]);
        var TheDGCell = PublicVRB.GetDataGridCell(DGCInf);
        return TheDGCell;
    }

使用方法:

private void DGR_SUB_INVOLST_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (DGR_SUB_INVOLST != null)
    {
        if (DGR_SUB_INVOLST.Items.Count > 0)
        {
               (e.Row.Item as INVOMonitor).CODE  = SomeValue;
     ((TextBlock)GetCellConfig("Binding_Name", row_index).Content).Text = (e.Row.Item as INVOMonitor).CODE.ToString();
        }
    }
}

相关问题