开发者

Programatically focus a UI element that was re-enabled in the prior statement

I'm developing a form and one of the requirements for it is that the majority of the fields be disabled until the first two have been completed. For the sake of usability I want it set up so after you tab out of the second field (Field_Two.LostFocus) the rest of the fields are enabled, displayed, and the first of those fields is focused. The code that I have currently sets all fields' visibility to visibility.hidden at startup. What it currently does is focus Field_One (next in the tab order of enabled fields), but I've confirmed that the conditions are properly satisfied and that execution proceeds past the return statement.

Field.beenFocused is a variable I've created that is initialized as false and 开发者_JS百科then set to true when the Field is focused for the first time, my Field class extends TextBox; all of my controls save Field_One and Field_Two are in Stackpanels.

C#

        void Field_Two_LostFocus(object sender, RoutedEventArgs e)
    {
        if (!Field_Three.beenFocused)
        {

            if (String.IsNullOrWhiteSpace(Field_One.Text) || String.IsNullOrWhiteSpace(Field_Two.Text))
                return;

            foreach (object u in ApplicationGrid.Children)
                if (u.GetType() == typeof(StackPanel))
                        ((StackPanel)u).IsEnabled = true;

            do { Field_Three.Focus(); }
            while (!Field_Three.beenFocused);
        }
    }


You could try enabling/disabling in a TextChanged event, rather than a LostFocus event.

Here's some sample code.

MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <TextBox Name="textBox1" TextChanged="ValidateInitialFieldsOnChange"
                 Height="23" Width="120" />
        <TextBox Name="textBox2" TextChanged="ValidateInitialFieldsOnChange"
                 Height="23" Width="120" />
        <TextBox Name="textBox3" IsEnabled="False" Height="23" Width="120" />
    </StackPanel>
</Window>

MainWindow.xaml.cs:

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ValidateInitialFieldsOnChange(object sender,
            TextChangedEventArgs e)
        {
            textBox3.IsEnabled = !string.IsNullOrWhiteSpace(textBox1.Text)
                && !string.IsNullOrWhiteSpace(textBox2.Text)
                ;
        }
    }
}

It would be fairly trivial to adapt this code to suit your scenario, and wouldn't require manually setting focus. You could probably rely on tab order instead.

But if tab order doesn't help, you can still use a combination of these approaches to solve the problem. Set focus in the LostFocus event, and enable in teh TextChanged event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜