Set focus on TextBox in WPF from view model

后端 未结 21 2702
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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();
        }
    }
    

提交回复
热议问题