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

后端 未结 5 423
我在风中等你
我在风中等你 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:06

    In addition to other answers: items aren't available in constructor of the control class (page / window / etc).

    If you want to access them after created, use Loaded event:

    public partial class MyUserControl : UserControl
    {
        public MyUserControl(int[] values)
        {
            InitializeComponent();
    
            this.MyItemsControl.ItemsSource = values;
    
            Loaded += (s, e) =>
            {
                for (int i = 0; i < this.MyItemsControl.Items.Count; ++i)
                {
                    // this.MyItemsControl.ItemContainerGenerator.ContainerFromIndex(i)
                }
            };
        }
    }
    

提交回复
热议问题