C# 未能聚焦WPF中ItemsControl的第一个子项

C# 未能聚焦WPF中ItemsControl的第一个子项,c#,wpf,C#,Wpf,我有一个ItemsControl,当ItemSource为null时,它会自动隐藏自己,并且在显示第一个子项之后,它会聚焦第一个子项。 我通过简单地将以下处理程序添加到“IsVisibleChanged”事件中,实现了正确的触发: 处理程序在第一次显示ItemsControl时总是工作得很好,但在第二次调用时,最后一行抛出InvalidOperationException 处理程序的工作方式是等待ItemContainerGenerator完成其作业(或超时1秒),然后获取ItemsContro

我有一个ItemsControl,当ItemSource为null时,它会自动隐藏自己,并且在显示第一个子项之后,它会聚焦第一个子项。 我通过简单地将以下处理程序添加到“IsVisibleChanged”事件中,实现了正确的触发:

处理程序在第一次显示ItemsControl时总是工作得很好,但在第二次调用时,最后一行抛出InvalidOperationException

处理程序的工作方式是等待ItemContainerGenerator完成其作业(或超时1秒),然后获取ItemsControl的第一个容器,并从该容器中聚焦名为“textBox”的元素

如果有人能告诉我出了什么问题,我会很高兴,因为唯一需要执行的是
if(first!=null)(first.ContentTemplate.FindName(“textBox”,first)as System.Windows.UIElement).Focus()

异常消息:
此操作仅在应用了此模板的元素上有效

StackTrace:
位于System.Windows.FrameworkTemplate.FindName(字符串名称,FrameworkElement templatedParent)

在***.d_u1.MoveNext()…

我找到了一种自己工作的方法,问题是容器没有完全初始化,所以我替换了最后一行以检查“IsLoaded”属性并执行一些额外的工作,还纠正了等待的任务

if ((bool)e.NewValue)
{
    ItemsControl control = sender as ItemsControl;
    if (control != null)
    {
        control.UpdateLayout();
        Task task = Task.Run(delegate ()
                    {
                        while (control.ItemContainerGenerator.Status == GeneratorStatus.NotStarted ||
                               control.ItemContainerGenerator.Status == GeneratorStatus.GeneratingContainers) ;
                    });

        if (await Task.WhenAny(task, Task.Delay(1000)) == task)
        {
            ContentPresenter first = control.ItemContainerGenerator.ContainerFromIndex(0) as ContentPresenter;
            if (first != null)
            {
                if (first.IsLoaded)
                {
                    (first.ContentTemplate.FindName("textBox", first) as System.Windows.UIElement).Focus();
                }
                else
                {
                    RoutedEventHandler onload = null;
                    onload = delegate
                    {
                        first.Loaded -= onload;
                        (first.ContentTemplate.FindName("textBox", first) as System.Windows.UIElement).Focus();
                    };
                    first.Loaded += onload;
                }
            }

        }
    }
} 

堆栈跟踪和引发异常的消息是什么?
if ((bool)e.NewValue)
{
    ItemsControl control = sender as ItemsControl;
    if (control != null)
    {
        control.UpdateLayout();
        Task task = Task.Run(delegate ()
                    {
                        while (control.ItemContainerGenerator.Status == GeneratorStatus.NotStarted ||
                               control.ItemContainerGenerator.Status == GeneratorStatus.GeneratingContainers) ;
                    });

        if (await Task.WhenAny(task, Task.Delay(1000)) == task)
        {
            ContentPresenter first = control.ItemContainerGenerator.ContainerFromIndex(0) as ContentPresenter;
            if (first != null)
            {
                if (first.IsLoaded)
                {
                    (first.ContentTemplate.FindName("textBox", first) as System.Windows.UIElement).Focus();
                }
                else
                {
                    RoutedEventHandler onload = null;
                    onload = delegate
                    {
                        first.Loaded -= onload;
                        (first.ContentTemplate.FindName("textBox", first) as System.Windows.UIElement).Focus();
                    };
                    first.Loaded += onload;
                }
            }

        }
    }
}