Fix NullReferenceException after UI Automation sets the query text
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_TextChanged1restores 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_KeyUpno 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:
| before | after | |
|---|---|---|
| Query box after the history command | keeps the automation text | shows the history query |
| Next key-up | process ends with NullReferenceException in QueryTextBox_KeyUp | no 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_TextChanged1restores the one-way binding toMainViewModel.QueryTextwhen the binding was replaced.- A new
PropertyChangedhandler forQueryTextrestores 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_KeyUpnow 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
MainWindowcode-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.