Why ItemContainerGenerator.ContainerFromIndex() returns null and how to avoid this behavior?

后端 未结 5 429
我在风中等你
我在风中等你 2020-12-06 16:31

I\'m using this snippet to analyze the rows I\'ve selected on a datagrid.

for (int i = 0; i < dgDetalle.Items.Count; i++)
{
    DataGridRow row = (DataGri         


        
5条回答
  •  粉色の甜心
    2020-12-06 17:05

    In my case grid.UpdateLayout(); didn't help an I needed a DoEvents() instead:

        DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
    
        if (row == null)
        {
    
            WPFTools.DoEvents();
    
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        }
    
    
        /// 
        /// WPF DoEvents
        /// Source: https://stackoverflow.com/a/11899439/1574221
        /// 
        public static void DoEvents()
        {
            var frame = new DispatcherFrame();
    
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
                new DispatcherOperationCallback(
                    delegate (object f)
                    {
                        ((DispatcherFrame)f).Continue = false;
                        return null;
                    }), frame);
    
            Dispatcher.PushFrame(frame);
        }
    

提交回复
热议问题