简体   繁体   English

使用MVVM在WPF中设置焦点

[英]Setting focus in WPF with MVVM

I have Grid with multiple Textboxes. 我有多个文本框的网格。 Depending on actions the user might take focus should be changed to one of the textboxes. 根据用户可能采取的操作,应将焦点更改为其中一个文本框。 My current solution uses a string property in the ViewModel and a data trigger in xaml to change focus. 我当前的解决方案使用ViewModel中的字符串属性和xaml中的数据触发器来更改焦点。 It works nicely but it seems a rather roundabout way to achieve this so I was wondering if it could be done in a clearner way? 它工作得很好,但它似乎是一个相当迂回的方式来实现这一点,所以我想知道它是否可以以一种更清晰的方式完成?

    <Grid.Style>
        <Style TargetType="Grid">
            <Style.Triggers>
                <DataTrigger Binding="{Binding FocusedItem}" Value="number">
                    <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=number}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding FocusedItem}" Value="name">
                    <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=name}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding FocusedItem}" Value="id">
                    <Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=id}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Style>

As you can see the value of the property and the name of the element is the same so I would like to do this ia single trigger instead of having one trigger per element. 正如您所看到的,属性的值和元素的名称是相同的,所以我想在单个触发器上执行此操作,而不是每个元素都有一个触发器。

Maybe someone can come up with a cleaner way? 也许有人可以想出一个更清洁的方式?

Thanks in advance 提前致谢

The way I handled setting focus in one of my projects was by using a focus extension (I apologize I do not remember where I saw the original post this is from). 我在我的一个项目中设置焦点的方式是使用焦点扩展(我道歉,我不记得我在哪里看到了原来的帖子)。

    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.Focus();
            }
        }
    }

And then in xaml file I use it as a Dependency Property: 然后在xaml文件中我将它用作依赖属性:

<TextBox Uid="TB1" FontSize="13" localExtensions:FocusExtension.IsFocused="{Binding Path=TB1Focus}" Height="24" HorizontalAlignment="Left" Margin="113,56,0,0" Name="TB_UserName" VerticalAlignment="Top" Width="165" Text="{Binding Path=TB1Value, UpdateSourceTrigger=PropertyChanged}" />

You can then use a binding to set the focus. 然后,您可以使用绑定来设置焦点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM