microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.36k stars 678 forks source link

AutosuggestBox , when ItemsSource bound to a CollectionViewSource, chooses 1st suggestion automatically on TextChanged event #1552

Open mdmozibur opened 5 years ago

mdmozibur commented 5 years ago

If not bound to a CollectionViewSource, it works. But when it's ItemsSource is bound to a CollectionViewSource, on the TextChanged event, it automatically chooses the first of the suggestions, thus, changing the text once again.

Steps to reproduce the bug

  1. Create a blank UWP C# app
  2. Change the MainPage.xaml to this:

    <Page
    x:Class="TestApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <Page.Resources>
        <CollectionViewSource x:Name="CVS"/>
    </Page.Resources>
    
    <AutoSuggestBox
        ItemsSource="{x:Bind CVS.View, Mode=OneWay}"
        TextChanged="SearchBox_TextChanged">
    </AutoSuggestBox>
    </Page>

and the MainPage.xaml.cs to this:

public sealed partial class MainPage : Page
{
    public List<string> GlobalShortcuts { get; }

    public MainPage()
    {
        GlobalShortcuts = new List<string>
        {
            "Help page",
            "Copy selected shapes",
            "Cut selected shapes",
            "Paste copied shapes",
            "Focus on searh field",
            "Create new file",
            "Open a file",
            "Save current file",
            "Save all files",
            "Undo",
            "Zoom in",
            "Zoom out",
            "Zoom in / out",
            "Redo",

            "Open shape list",
            "Open command window",
            "Offset mode",
            "Select mode",
            "Draw mode",
            "Scissor mode",
            "Pattern mode",
            "Mirror mode",
            "Move mode",
            "Dimension mode",
            "Rotation mode",
            "Binding mode",
        };

        this.InitializeComponent();
    }

    private void SearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
    {
        CVS.Source = GlobalShortcuts.ToList();
    }
}
  1. Now build and run the app.
  2. type anything in the AutoSuggestBox. Notice that the 1st suggestion is chosen automatically.

Screenshots output

Version Info

I produced this with the AutoSuggestBox from Windows.UI.Xaml.Control , but I think this also happens to the one of Microsoft.UI.Xaml.Control

Windows 10 version Saw the problem?
May 2019 Update (18362) Yes
Device form factor Saw the problem?
Desktop Yes

Edit: Checking for args.Reason doesn't prevent this to happen:

private void SearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
        if (args.Reason != AutoSuggestionBoxTextChangeReason.UserInput)
            return;
    CVS.Source = GlobalShortcuts.ToList();
}
mdmozibur commented 5 years ago

I am thinking of an ugly work around, which involves checking the args.Reason in the TextChanged event handler. I hope this critical bug will be addressed as soon as possible.

chrisglein commented 4 years ago

The information in the docs indicate you should be checking the args.Reason. Follow that guideline and reactivate if you're still having problems.

mdmozibur commented 4 years ago

@chrisglein I have checked, still happens. Using the following code:

private void SearchBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
    if (args.Reason != AutoSuggestionBoxTextChangeReason.UserInput)
        return;

    CVS.Source = GlobalShortcuts.ToList();
}

This is the result:

op

Please re-open the issue. This is a bug!

mdmozibur commented 4 years ago

I figured that if set the UpdateTextOnSelect property to false then this becomes somewhat manageable, but still.. the SuggestionChosen event fires without me selecting anything.

chrisglein commented 4 years ago

@MuziburRahman Sorry, my mistake for misreading that. I thought you were adverse to checking Reason. We'll take a look.

Just to set expectations. AutoSuggestBox's control code isn't in WinUI so this likely needs WinUI3 to fix unless it's a bug in the control style (which we can patch with WinUI2)

andywcoder commented 1 year ago

I just noticed that it still happens with WinUI 3. But in my case, only the first time when items get bound to the control, the first item in the suggestion list gets selected. After that it works as one would expect.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

Balkoth commented 1 year ago

Still an issue...