Home > Backend Development > C++ > How to Set Focus on a WPF TextBox from its ViewModel?

How to Set Focus on a WPF TextBox from its ViewModel?

Mary-Kate Olsen
Release: 2025-01-26 02:02:09
Original
490 people have browsed it

How to Set Focus on a WPF TextBox from its ViewModel?

Set the TextBox focus in ViewModel in the WPF

In the development of WPF applications, sometimes the focus of specific controls is required from ViewModel. This can be implemented by combining with additional attributes and binding techniques.

Avoid direct reference UI elements in ViewModel

In the sample code, directly access the UI element in the view. It is usually not recommended to directly reference UI elements in the ViewModel layer, because this will cause the code to be tightly coupled with the UI and reduce testability.

Use additional attributes to focus on management cs.txtCompanyID

A better method is to use additional attributes, which can be bound to the ViewModel attribute. In this example, we can create an additional attribute called . It accepts a Boolean value and indicates whether the associated control should have a focus.

The following is a sample implementation of additional attributes:

IsFocused

Bind the additional attributes to ViewModel

IsFocused

After with the additional attribute, you can bind it to the viewmodel attribute with the focus of the indicator control. In your view (XAML), you can bind
<code class="language-csharp">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(); // 忽略false值
        }
    }
}</code>
Copy after login
attributes to the corresponding viewmodel attribute:

<.> Use .NET source code to debug

IsFocused If you encounter problems when setting the focus, consider debugging the .NET source code to understand its behavior in depth. Here, you can observe how the underlying framework handles the focus and may identify any differences in the custom implementation.

The above is the detailed content of How to Set Focus on a WPF TextBox from its ViewModel?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template