I'm experiencing an issue where, when I "tab out" of a specific AutoSuggestBox, it discards its value somewhere between its LosingFocus and LostFocus events.
I've only encountered this issue on one specific AutoSuggestBox within my app. Other instances with similar/identical functionality behave exactly as expected.
Minimal Reproducable Example
<AutoSuggestBox Text="{x:Bind ViewModel.Value, Mode=TwoWay}"
ItemsSource="{x:Bind ViewModel.VisibleValues, Mode=OneWay}"
TextMemberPath="TextMember" DisplayMemberPath="TextMember"
LosingFocus="TestLosingFocus" LostFocus="TestLostFocus" />
// ...
private void TestLosingFocus(UIElement sender, LosingFocusEventArgs args)
{
string senderText = (sender as AutoSuggestBox).Text; // "abc"
string vmText = ViewModel.Value; // "abc"
}
private void TestLostFocus(object sender, RoutedEventArgs e)
{
string senderText = (sender as AutoSuggestBox).Text; // ""
string vmText = ViewModel.Value; // ""
}
// ...
Enter a value to the AutoSuggestBox, then hit "tab" to move to the next item in the tab order. Notably, I do not encounter this bug when using the mouse to remove focus from the AutoSuggestBox.
What Happens
When pressing "tab" to move focus to the next element, the AutoSuggestBox fires its LosingFocus event, which runs as expected: Both the sender's text and the property on the view model have the same value as the text that is visible within the AutoSuggestBox.
Stepping over, out of this method, the following function (which is part of generated code) runs without entering the catch block. It is not possible to step into the statement inside the try block, as the debugger merely steps over it.
namespace WinRT.GenericTypeInstantiations;
// ...
internal static class Windows_Foundation_TypedEventHandler_2_Microsoft_UI_Xaml_UIElement__Microsoft_UI_Xaml_Input_LosingFocusEventArgs
{
// ...
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
private static unsafe int Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr args)
{
try
{
global::ABI.Windows.Foundation.TypedEventHandlerMethods<global::Microsoft.UI.Xaml.UIElement, IntPtr, global::Microsoft.UI.Xaml.Input.LosingFocusEventArgs, IntPtr>.Abi_Invoke(thisPtr, global::ABI.Microsoft.UI.Xaml.UIElement.FromAbi(sender), global::ABI.Microsoft.UI.Xaml.Input.LosingFocusEventArgs.FromAbi(args));
}
catch (Exception __exception__)
{
global::WinRT.ExceptionHelpers.SetErrorInfo(__exception__);
return global::WinRT.ExceptionHelpers.GetHRForException(__exception__);
}
return 0;
}
// ...
}
Next, ViewModel.Value's setter is hit. This point cannot be reached via stepping over/through with the debugger, but instead with a separate breakpoint - however, the previous generated code call is in the call stack when the setter is hit. The value that is assigned is an empty string.
Then (stepping on from the end of the generated function), the AutoSuggestBox fires LostFocus. Checking within this method, we see that ViewModel.Value has indeed been set to an empty string.
What I've Tried
I've tried removing all methods, styles, and anything else that might have an effect on the AutoSuggestBox. Additionally, I've removed every other line of code that could possibly set the value of ViewModel.Value. Nothing I've tried has made any change, and this issue has had me stumped for nearly a month now.