【问题标题】:WinUI3-MVVM: Setting UI control focus from ViewModelWinUI3-MVVM:从 ViewModel 设置 UI 控制焦点
【发布时间】:2023-01-07 09:29:24
【问题描述】:

在 MVVM 架构中设置控制焦点的好做法是什么。我试过 FocusBehavior 但它没有用。什么是最好的解决方案?

焦点行为.cs

public class FocusBehavior
{
    public static void SetIsFocused(UIElement element, bool value)
    {
        element.SetValue(IsFocusedProperty, value);
    }
    public static bool GetIsFocused(UIElement element)
    {
        return (bool)element.GetValue(IsFocusedProperty);
    }

    public static readonly DependencyProperty IsFocusedProperty =
        DependencyProperty.RegisterAttached(
            "IsFocused",
            typeof(bool),
            typeof(FocusBehavior),
            new PropertyMetadata(false, (d, e) =>
            {
                if ((bool)e.NewValue)
                {
                    var uiElement = d as UIElement;
                    if (uiElement != null)
                    {
                        uiElement.Focus(FocusState.Pointer);
                    }
                }
            })
        );
}

【问题讨论】:

  • 这段除了将焦点放在一个元素上之外什么都不做的代码与“MVVM 中焦点的良好实践”之间有什么关系?你应该澄清一下。 stackoverflow.com/help/how-to-ask

标签: windows winui-3


【解决方案1】:

正如 cmets 中提到的那样,您应该进一步澄清您的问题。 我还想提一下,“焦点”是一项 UI 功能。 ViewModels 不应该担心控件是否有焦点。如果可能,请尝试在后面的代码中更改焦点。但在某些情况下,您可能需要根据 ViewModel 更改焦点。

为了使您的代码正常工作,您需要处理焦点事件:

public class FocusBehavior
{
    public static void SetIsFocused(UIElement element, bool value)
    {
        element.SetValue(IsFocusedProperty, value);
    }

    public static bool GetIsFocused(UIElement element)
    {
        return (bool)element.GetValue(IsFocusedProperty);
    }

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

    private static void OnIsFocusedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is UIElement uiElement &&
            e.NewValue is bool newValue)
        {
            if (newValue is true)
            {
                uiElement.Focus(FocusState.Programmatic);
                uiElement.LostFocus += UiElement_LostFocus;
            }
            else
            {
                uiElement.GotFocus += UiElement_GotFocus;
            }
        }
    }

    private static void UiElement_LostFocus(object sender, RoutedEventArgs e)
    {
        if (sender is UIElement uiElement)
        {
            uiElement.SetValue(IsFocusedProperty, false);
            uiElement.LostFocus -= UiElement_LostFocus;
        }
    }

    private static void UiElement_GotFocus(object sender, RoutedEventArgs e)
    {
        if (sender is UIElement uiElement)
        {
            uiElement.SetValue(IsFocusedProperty, true);
            uiElement.GotFocus -= UiElement_GotFocus;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-07-17
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 2011-06-09
    相关资源
    最近更新 更多