Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF ItemsControl-如何知道ItemsControl何时完成加载,以便我可以集中第一个?_Wpf_Mvvm_Wpf Controls - Fatal编程技术网

WPF ItemsControl-如何知道ItemsControl何时完成加载,以便我可以集中第一个?

WPF ItemsControl-如何知道ItemsControl何时完成加载,以便我可以集中第一个?,wpf,mvvm,wpf-controls,Wpf,Mvvm,Wpf Controls,在我的视图中有一个ItemsControl,它绑定到来自ViewModel的ObservableCollection。该集合被填充,然后引发一个从VM到view的事件(想想搜索结果和SearchFinished事件)。 如何将键盘焦点移动到ItemsControl中的第一项? 我正在使用MVVM模式在使用行为之前,我已经这样做了: public sealed class FocusBehavior : Behavior<FrameworkElement> { protecte

在我的视图中有一个ItemsControl,它绑定到来自ViewModel的ObservableCollection。该集合被填充,然后引发一个从VM到view的事件(想想搜索结果和SearchFinished事件)。 如何将键盘焦点移动到ItemsControl中的第一项?
我正在使用MVVM模式

在使用行为之前,我已经这样做了:

public sealed class FocusBehavior : Behavior<FrameworkElement>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.Loaded += OnLoaded;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.Loaded -= OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        AssociatedObject.Focus();
    }
}
公共密封类焦点行为:行为
{
受保护的覆盖无效附加()
{
base.onatached();
AssociatedObject.Loaded+=已加载;
}
附加时受保护的覆盖无效()
{
base.OnDetaching();
AssociatedObject.Loaded-=已加载;
}
已加载专用void(对象发送方,RoutedEventArgs e)
{
AssociatedObject.Focus();
}
}
然后在XAML中使用它,如下所示:

<TextBox x:Name="UsernameTextBox"
         Text="{Binding Path=Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
         KeyboardNavigation.TabIndex="0">
    <i:Interaction.Behaviors>
        <b:FocusBehavior />
    </i:Interaction.Behaviors>
</TextBox>


您需要对System.Windows.Interactivity程序集的引用才能拉入交互命名空间。

我找到了解决方案

private void VerifiValue_Loaded(object sender, RoutedEventArgs e)
    {
        if (verificationIC.Items.Count > 0)
        {
            UIElement uiElement = (UIElement)verificationIC.ItemContainerGenerator.ContainerFromIndex(0);

            TextBox t = GetChild<TextBox>(uiElement);
            t.Focus();
            t.SelectAll();
        }
    }

    public T GetChild<T>(DependencyObject obj) where T : DependencyObject
    {
        DependencyObject child = null;
        for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child.GetType() == typeof(T))
            {
                break;
            }
            else if (child != null)
            {
                child = GetChild<T>(child);
                if (child != null && child.GetType() == typeof(T))
                {
                    break;
                }
            }
        }
        return child as T;
    }
private void verificationvalue\u加载(对象发送方,路由目标)
{
如果(0.Items.Count>0)
{
UIElement UIElement=(UIElement)Verification.ItemContainerGenerator.ContainerFromIndex(0);
文本框t=GetChild(uiElement);
t、 焦点();
t、 选择全部();
}
}
公共T GetChild(DependencyObject对象),其中T:DependencyObject
{
DependencyObject子对象=null;
for(Int32 i=0;i
也许您应该尝试ItemsControl的LayoutUpdated事件,在其处理程序中找到它的第一个子项并使用键盘。Focus(子项)如何在viewmodel中创建LayoutUpdated事件?您可以在视图的代码后面创建它,它没有问题,并且没有违反MVVM模式,因为它的视图特定行为是您想要的。当==ContainerGenerated时,您可以使用触发焦点事件。您可以给我一个ItemsControl.ItemContainerGenerator.Status的示例代码吗?