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

时间:2015-11-18 07:16:48

标签: c# wpf datagrid

在我的应用程序中,有一个datagrid包含listbox作为datagrid Cell.After更新datagrid ItemsSource后,我还要更新listbox itemsSource。

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

如上所述,在更新datagrid ItemsSource之后,还要更新为datagrid单元格的列表框的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 = grid.ItemContainerGenerator.ContainerFromItem(item)as   DataGridRow;

问题是row总是返回null。

1 个答案:

答案 0 :(得分:0)

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

如果未呈现该项目,则尚未生成该项目。

添加到您的DataGrid:

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

在此处阅读更多内容:DataGrid.EnableRowVirtualization Property