键入一个字符后,WPF文本框失去焦点

时间:2016-07-01 18:44:02

标签: c# wpf wpf-textbox

我是WPF的新手。我被分配了一个bug来修复。知道为什么文本框会在搜索栏中只输入一个字符后查找匹配的模型编号时会失去焦点。我不知道要发布哪些代码来帮助你们。

    <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;
        }));
    }

1 个答案:

答案 0 :(得分:1)

UpdateSourceTrigger设置为PropertyChanged,因此每次输入字符时,ModelName都会更新。我怀疑那里的setter中可能会有一些代码导致焦点丢失。您可以尝试将UpdateSourceTrigger更改为“Explicit”或“LostFocus”。

要找到右键单击后面的代码,然后选择“查看代码”。但是它可能使用MVVM模式,因此将有另一个包含视图逻辑的类。尝试右键单击模型名称(在Text =“{Binding Path = ModelName )中并选择”转到定义“(我相信此功能仅在VS2013及更新版本中可用)。