Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null check for autocomplete box #3741

Open
AmeerMansour opened this issue Dec 13, 2024 · 2 comments
Open

Null check for autocomplete box #3741

AmeerMansour opened this issue Dec 13, 2024 · 2 comments
Labels
Waiting on feedback Additional information is needed. Stale items with this label may be closed.

Comments

@AmeerMansour
Copy link

Bug explanation

I start type in the autocomplete boxthen when I select one of the suggestions it crashes
this is my xmal
image

and it crash here for nor selecteditem being null
image

Version

5.1

@AmeerMansour AmeerMansour added bug evaluation required Items is pending review or evaluation by the team labels Dec 13, 2024
@corvinsz
Copy link
Contributor

@AmeerMansour I can't reproduce your problem. Can you share more information about your project and/or check if you have implemented the AutoSuggestBox correctly?
Here is a minimal setup to get the AutoSuggestBox to work:

<Window x:Class="MDIX3741.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:local="clr-namespace:MDIX3741"
        xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="800"
        Height="450"
        Style="{StaticResource MaterialDesignWindow}"
        mc:Ignorable="d">
    <materialDesign:AutoSuggestBox Margin="5"
                                   VerticalAlignment="Center"
                                   materialDesign:HintAssist.Hint="Value"
                                   materialDesign:TextFieldAssist.HasClearButton="True"
                                   Style="{StaticResource MaterialDesignOutlinedAutoSuggestBox}"
                                   Suggestions="{Binding Suggestions}"
                                   Text="{Binding MyValue, UpdateSourceTrigger=PropertyChanged}">

        <materialDesign:AutoSuggestBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding .}" />
            </DataTemplate>
        </materialDesign:AutoSuggestBox.ItemTemplate>
    </materialDesign:AutoSuggestBox>
</Window>

Code behind:

public partial class MainWindow : Window
{
	public MainWindow()
	{
		InitializeComponent();
		this.DataContext = new MainViewModel();
	}
}

public partial class MainViewModel : ObservableObject
{
	private static readonly List<string> _allSuggestions = ["1", "11", "112", "1333", "444"];

	[ObservableProperty]
	private string _myValue;

	partial void OnMyValueChanged(string value)
	{
		if (string.IsNullOrEmpty(value))
		{
			Suggestions = _allSuggestions;
			return;
		}

		Suggestions = _allSuggestions.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase))
									.ToList();
	}

	private IReadOnlyList<string> _suggestions;
	public IReadOnlyList<string> Suggestions
	{
		get => _suggestions;
		set => SetProperty(ref _suggestions, value);
	}
}

Make sure to install the CommunityToolkit.Mvvm to get this code to work.

@corvinsz
Copy link
Contributor

corvinsz commented Jan 8, 2025

@AmeerMansour did this answer your question or does your problem persist?

@corvinsz corvinsz added Waiting on feedback Additional information is needed. Stale items with this label may be closed. and removed bug evaluation required Items is pending review or evaluation by the team labels Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Waiting on feedback Additional information is needed. Stale items with this label may be closed.
Projects
None yet
Development

No branches or pull requests

2 participants