Fix NullReferenceException after UI Automation sets the query text

DO
3d ago
+17-1

Fixes #4768.

Cause

QueryTextBox.Text is bound one-way to QueryText. WPF's TextBoxAutomationPeer implements ValuePattern.SetValue as owner.Text = value, and a local value replaces a one-way binding. After that the box no longer follows the view model, and QueryTextBox_KeyUp dereferences the missing binding expression as soon as QueryText differs from the box.

Change

  • QueryTextBox_TextChanged1 restores the one-way binding when it finds it gone. The view model has just received the new text, so the box does not change.
  • QueryTextBox_KeyUp no longer dereferences a null binding expression.

A null check alone stops the exception but leaves the box detached from QueryText, so both are included.

Verification

Debug build of dev on Windows 11, portable UserData, no plugins. The query was set with ValuePattern.SetValue, and the cycle-history command (remapped to F9 in the test profile) was used to change QueryText from the view model:

beforeafter
Query box after the history commandkeeps the automation textshows the history query
Next key-upprocess ends with NullReferenceException in QueryTextBox_KeyUpno exception

Both columns were run twice with the same result. An isolated WPF check of the same handlers gave the same picture, including the detached box with only the null check.

Not covered: no unit test was added, since the handlers live in MainWindow code-behind, and the solution's test project and the plugins were not built or run.


Summary by cubic

Summary of changes

Fixes a NullReferenceException in the query box that occurred after UI Automation's ValuePattern.SetValue set the query text directly, because setting Text replaces the one-way binding with a local value.

  • QueryTextBox_TextChanged1 restores the one-way binding to MainViewModel.QueryText when the binding was replaced.
  • A new PropertyChanged handler for QueryText restores the binding on any subsequent query change, so the box keeps following the view model even when the next change does not come from typing.
  • QueryTextBox_KeyUp now uses ?.UpdateSource() instead of dereferencing a binding expression that can be null.
  • No existing logic was removed.
  • No meaningful memory usage impact; the change only rebinds an existing property.
  • No security impact.
  • No unit tests were added; the handlers live in MainWindow code-behind.

Release Note

Fixes a crash that could happen when the query text was set by another application, and makes sure the query box keeps showing the latest query.