Question

So I'm having this weird problem. I have a TextBox bound to the ViewModel :

<TextBox DockPanel.Dock="Right" Text="{Binding Path=FilterText, UpdateSourceTrigger=PropertyChanged}"/>

With the content of FilterText, I am filtering a ListBox (which is bound to an ObservableCollection).

Now everything works fine. But after each keystroke, the cursor position of this TextBox keeps resetting to the beginning. And this is happening only with this particular TextBox. I have other bound TextBox working just fine.

I googled it, and found that this happens in .NET4.0. But mainly when there is a string formatting or string to double conversion is involved. But FilterText is simple String property and no double variable is involved anywhere.

Here is the code :

private String _filterText;
public String FilterText
{
    get { return _filterText; }
    set
    {
        if (_filterText != value)
        {
            _filterText = value;
            RaisePropertyChanged("FilterText");
            FilterList(); //this method works fine
        }
    }
}

Can someone please explain why this is happening? The solutions suggested somewhere else includes changing UpdateSourceTrigger to LostFocus or using an attached behavior to manually set the cursor location. Is there any other simpler solution? Or is this just a bug?

Était-ce utile?

La solution

Got your source-code and could reproduce the issue. However it wasn't one of those .NET 4 issues cos it had the same issue in .NET 4.5. Actually turns out it's not an issue in .NET at all.

In ManageQuestionViewModel.cs

switch

public void Initialize() {
...
  this.ViewCore.FocusQuestionData();
}

to

public void Initialize() {
...
  // this.ViewCore.FocusQuestionData();
}

you're done :)

oh and you do not need that ResetCaretBehavior you've added to try and work-around this anymore as well

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top