Set focus on TextBox in WPF from view model

后端 未结 21 2256
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 06:21

I have a TextBox and a Button in my view.

Now I am checking a condition upon button click and if the condition turns out to be false, displ

相关标签:
21条回答
  • 2020-11-22 06:56

    I use WPF / Caliburn Micro a found that "dfaivre" has made a general and workable solution here: http://caliburnmicro.codeplex.com/discussions/222892

    0 讨论(0)
  • 2020-11-22 06:57

    Let me answer to your question in three parts.

    1. I'm wondering what is "cs.txtCompanyID" in your example? Is it a TextBox control? If yes, then you are on a wrong way. Generally speaking it's not a good idea to have any reference to UI in your ViewModel. You can ask "Why?" but this is another question to post on Stackoverflow :).

    2. The best way to track down issues with Focus is... debugging .Net source code. No kidding. It saved me a lot of time many times. To enable .net source code debugging refer to Shawn Bruke's blog.

    3. Finally, general approach that I use to set focus from ViewModel is Attached Properties. I wrote very simple attached property, which can be set on any UIElement. And it can be bound to ViewModel's property "IsFocused" for example. Here it is:

      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(); // Don't care about false values.
              }
          }
      }
      

      Now in your View (in XAML) you can bind this property to your ViewModel:

      <TextBox local:FocusExtension.IsFocused="{Binding IsUserNameFocused}" />
      

    Hope this helps :). If it doesn't refer to the answer #2.

    Cheers.

    0 讨论(0)
  • 2020-11-22 06:59

    I found Crucial's solution to the IsVisible problem very useful. It didn't completely solve my problem, but some extra code following the same pattern for the IsEnabled pattern did.

    To the IsFocusedChanged method I added:

        if (!fe.IsEnabled)
        {
            fe.IsEnabledChanged += fe_IsEnabledChanged;
        }
    

    And here's the handler:

    private static void fe_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var fe = (FrameworkElement)sender;
        if (fe.IsEnabled && (bool)((FrameworkElement)sender).GetValue(IsFocusedProperty))
        {
            fe.IsEnabledChanged -= fe_IsEnabledChanged;
            fe.Focus();
        }
    }
    
    0 讨论(0)
提交回复
热议问题