Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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
C# wpf MVVM焦点光标位于文本框上_C#_Wpf_Mvvm - Fatal编程技术网

C# wpf MVVM焦点光标位于文本框上

C# wpf MVVM焦点光标位于文本框上,c#,wpf,mvvm,C#,Wpf,Mvvm,在WPF窗口中,我试图在默认情况下将光标放在其中一个文本框上。 在阅读了一些问题和答案后,我尝试了以下方法: xaml: 打开窗口时,我希望看到文本框FID上的光标闪烁。 但是,这个文本框上根本没有光标。 调试显示我正在检查cs代码,将值设置为true。 有什么想法吗?解决方案包括: 1.添加FocusExtension类。 2.焦点和键盘。焦点在Dispatcher.BeginInvoke中 政务司司长 .xaml 试着找到答案。添加FocusExtension类,移除StackPanel

在WPF窗口中,我试图在默认情况下将光标放在其中一个文本框上。 在阅读了一些问题和答案后,我尝试了以下方法:

xaml:

打开窗口时,我希望看到文本框FID上的光标闪烁。 但是,这个文本框上根本没有光标。 调试显示我正在检查cs代码,将值设置为true。 有什么想法吗?

解决方案包括: 1.添加FocusExtension类。 2.焦点和键盘。焦点在Dispatcher.BeginInvoke中

政务司司长

.xaml


试着找到答案。添加
FocusExtension
类,移除StackPanel并执行此操作:
对我有效。可能存在重复的
<StackPanel Grid.Row="1"
    <StackPanel.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding UserShouldEditValueNow}" Value="true">
                    <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=FID}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    <TextBox Name ="FID" Text="{Binding FixID, UpdateSourceTrigger=PropertyChanged}" 
    FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
</StackPanel>
this.UserShouldEditValueNow = true;
public static class FocusExtension
    {
        public static bool GetIsFocused(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsFocusedProperty);
        }

        public static void SetIsFocused(DependencyObject obj, bool value)
        {
            obj.SetValue(IsFocusedProperty, value);
        }

        public static readonly DependencyProperty IsFocusedProperty =
            DependencyProperty.RegisterAttached(
                "IsFocused", typeof(bool), typeof(FocusExtension),
                new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));

        private static void OnIsFocusedPropertyChanged(
            DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            var uie = (UIElement)d;
            if ((bool)e.NewValue)
            {
                uie.Dispatcher.BeginInvoke(
                    new Action(
                        delegate{
                            uie.Focus(); 
                            Keyboard.Focus(uie);
                        }
                    )
                );
            }
        }
    }
        <TextBox Text="{Binding FixID, UpdateSourceTrigger=PropertyChanged}"  viewModels:FocusExtension.IsFocused="{Binding UserShouldEditValueNow}" />