HelloWorld
HelloWorld

Reputation: 69

WPF Textbox loses focus after typing one character

I am extremely new to WPF. I am assigned a bug to fix. Any idea why a textbox would loose focus after typing just one character into a search bar that looks for matching models numbers. I don't know which code to post to help you guys.

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition
                Width="100" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Label
            Content="Model Name:" />
        <TextBox
            Grid.Column="1"
            Text="{Binding Path=ModelName, UpdateSourceTrigger=PropertyChanged, 
                   ValidatesOnDataErrors=True}"
            IsReadOnly="{Binding IsLoading}"
            Style="{StaticResource FieldTextBox}"
            MaxLength="{Binding ModelNameMaxLength}">
            <TextBox.InputBindings>
                <KeyBinding
                    Key="Enter"
                    Command="vmdls:ModelManagerViewModel.EditModel" />
                <KeyBinding
                    Key="Up"
                    Command="vmdls:ModelManagerViewModel.MoveSelectionUp"/>
                <KeyBinding
                    Key="Down"
                    Command="vmdls:ModelManagerViewModel.MoveSelectionDown" />
            </TextBox.InputBindings>
        </TextBox>
    </Grid>



    private string _modelName;

    public string ModelName
    {
        get { return _modelName; }
        set
        {
            if (_modelName == value)
                return;

            _modelName = value;

            RefreshModels();
        }
    }

     private void RefreshModels()
    {
        if (ModelName.SafeTrim() == string.Empty)
            Models = new ObservableCollection<string>();
        else
        {
            IsLoading = true;
            Models = new ObservableCollection<string>(new string[] { "Loading ..." });

            var workerThread = new Thread(new ThreadStart(GetModelsInternal)) 
          { IsBackground = true };

            workerThread.Start();
        }
    }

    private void GetModelsInternal()
    {
        IWaitIndicator waitIndicator = null;

        var dispatcher = View.ViewElement.Dispatcher;

        dispatcher.Invoke(new ThreadStart(() =>
        {
            waitIndicator = View.GetService<IWaitIndicatorFactory>().CreateWaitIndicator(); 
        }));

        var newModels = new string[] { };

        try
        {
            using (var proxy = new ModelSvcClient())
                newModels = proxy.FindModelsByName(ModelName);
        }
        catch (Exception vx)
        {
            View.GetService<IDisplayMessage>().ShowErrorMessage(vx.Message);
        }

        dispatcher.Invoke(new ThreadStart(() =>
        {
            _models = new ObservableCollection<string>(newModels);
            SelectedModelsItem = _models.FirstOrDefault();

            waitIndicator.Dispose();

            OnPropertyChanged(() => Models);
            IsLoading = false;
        }));
    }

Upvotes: 0

Views: 1017

Answers (1)

AXG1010
AXG1010

Reputation: 1962

UpdateSourceTrigger is set to PropertyChanged, so every time a character gets typed, ModelName gets updated. I would suspect that there might be some code in the setter there that is causing the focus to get lost. You can try changing UpdateSourceTrigger to either "Explicit" or "LostFocus".

To find the code behind just right click, and select "View Code". However it is likely using an MVVM pattern so there will be another class that contains the logic for the view. Try right clicking on Model name (in Text="{Binding Path=ModelName) and selecting "Go To Definition" (I believe this feature is only available in VS2013 and newer).

Upvotes: 1

Related Questions