更新wpf中的ItemsSource后如何获取DataGrid行

本文关键字:获取 DataGrid 何获取 中的 wpf ItemsSource 更新 | 更新日期: 2023-09-27 18:22:33

在我的应用程序中,有一个数据网格包含listbox作为数据网格单元格。在更新数据网格ItemsSource后,我还想更新listbox ItemsSource。

private void DataUpdate()
if (_dtWorkTime.Rows.Count > 0)
{
     taskDetailGrid.ItemsSource = _dtWorkTime.DefaultView;
     taskDetailGrid.Items.Refresh();
     taskDetailGrid.UpdateLayout();
     LisBoxDataUpdate(SelectContacts);
}

如上所述,在更新数据网格ItemsSource之后,还更新到作为数据网格单元格的列表框的ItemsSource。

private void LisBoxDataUpdate(ObservableCollection<ContactInfo> SelectContacts)
        {
            if (SelectContacts != null)
            {
                var row_list = GetDataGridRows(taskDetailGrid);
                foreach (DataGridRow single_row in row_list)
                {
                    if (single_row == null) break;
                    if (single_row.IsSelected == true)
                    {
                        ListBox memberGrid = FindChild<ListBox>(single_row, "memberListBox");
                        memberGrid.ItemsSource = SelectContacts;
                        Button btnMemberAdd = FindChild<Button>(single_row, "btnMemberAdd");
                        if (SelectContacts.Count <= 0)
                        {
                            memberGrid.Visibility = Visibility.Hidden;
                            btnMemberAdd.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            memberGrid.Visibility = Visibility.Visible;
                            btnMemberAdd.Visibility = Visibility.Hidden;
                        }
                        break;
                    }
                }
            }
        }
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;
                }
            }
        }

XAML代码如下。

<DataGrid x:Name="taskDetailGrid" Margin="0,0,-0.333,0.333" VerticalScrollBarVisibility="Hidden"
                  HorizontalScrollBarVisibility="Hidden" HeadersVisibility="None" RowHeight="80"
                  AutoGenerateColumns="False" CanUserAddRows="False" BorderThickness="1" Background="#FFFFFF"
                  ScrollViewer.CanContentScroll="True" AlternationCount="2"
                  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionChanged="taskDetailGrid_SelectionChanged" Grid.ColumnSpan="2" DataContextChanged="taskDetailGrid_DataContextChanged">
<DataGrid.Columns>
                    <DataGridTemplateColumn x:Name="userControlColumn" Header="Column1" IsReadOnly="True" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel x:Name="stkPanel">
<ListBox  x:Name="memberListBox" Margin="440,-55,0,0"  HorizontalAlignment="Left" VerticalAlignment="Top"  ItemsSource="{Binding SelectContacts}" 
                                        ItemTemplate="{StaticResource ContactInfoTemplate}" Visibility="Hidden" Width="150" PreviewMouseDown="memberListBox_PreviewMouseDown">
                                    </ListBox>
                                    <TextBox x:Name="txtComment" Text="{Binding ReportComment}" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,-55,3,0" Width="250" Height="50"
                                             TextWrapping="Wrap" AcceptsReturn="True"/>
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
</DataGrid.Columns>
            </DataGrid>

var row=网格。ItemContainerGenerator.ContainerFromItem(项)为DataGridRow;

问题是行总是返回null。

更新wpf中的ItemsSource后如何获取DataGrid行

DataGrid的默认设置是使用虚拟化加载项,这意味着行按需加载。

当项目未渲染时,项目尚未生成。

添加到您的DataGrid:

EnableRowVirtualization="False"
VirtualizingStackPanel.IsVirtualizing="False" 

点击此处阅读更多:DataGrid.EnableRowVirtualization属性