简体   繁体   中英

How to Get DataGrid Rows after updating ItemsSource in wpf?

In my application, there is a datagrid that include listbox as the datagrid Cell.After updating the datagrid ItemsSource,I want to also update the listbox itemsSource.

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

As the above, after updating the datagrid ItemsSource, also update to the itemssource of the listbox that is datagrid cell.

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 code is as the following.

<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 = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;

The problem is that row is always return null.

The default for DataGrid is to load the items with Virtualization , Which means the rows load on demand.

When the item isn't rendered, the item is NOT generated yet.

Add to your DataGrid:

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

Read more here : DataGrid.EnableRowVirtualization Property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM