Skip to content

Fix AutoSuggestBox not setting SelectedItem #3847

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

Merged
merged 2 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/MaterialDesignThemes.Wpf/AutoSuggestBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ public object SelectedItem
set => SetValue(SelectedItemProperty, value);
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register(nameof(SelectedItem), typeof(object), typeof(AutoSuggestBox), new PropertyMetadata(default(object)));
DependencyProperty.Register(
nameof(SelectedItem),
typeof(object),
typeof(AutoSuggestBox),
new FrameworkPropertyMetadata(default(object), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));


public object SelectedValue
Expand Down Expand Up @@ -254,6 +258,7 @@ private bool CommitValueSelection(object? selectedValue)
{
CaretIndex = Text.Length;
}
SetCurrentValue(SelectedItemProperty, selectedValue);
CloseAutoSuggestionPopUp();
var args = new RoutedPropertyChangedEventArgs<object?>(oldValue, Text)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
materialDesign:HintAssist.HelperText="Name"
materialDesign:TextFieldAssist.HasClearButton="True"
Text="{Binding AutoSuggestText, UpdateSourceTrigger=PropertyChanged}"
Suggestions="{Binding Suggestions}">
Suggestions="{Binding Suggestions}"
SelectedItem="{Binding SelectedItem}"
>
</materialDesign:AutoSuggestBox>
<!--
<ComboBox ItemsSource="{Binding Suggestions}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using CommunityToolkit.Mvvm.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using CommunityToolkit.Mvvm.ComponentModel;

namespace MaterialDesignThemes.UITests.Samples.AutoSuggestBoxes;

Expand All @@ -26,6 +26,9 @@ public partial class AutoSuggestTextBoxWithCollectionViewViewModel : ObservableO
[ObservableProperty]
private string? _autoSuggestText;

[ObservableProperty]
private string? _selectedItem;

partial void OnAutoSuggestTextChanged(string? oldValue, string? newValue)
{
if (!string.IsNullOrWhiteSpace(newValue))
Expand All @@ -36,19 +39,19 @@ partial void OnAutoSuggestTextChanged(string? oldValue, string? newValue)
{
Suggestions.Filter = null;
}
base.OnPropertyChanged(nameof(Suggestions));
OnPropertyChanged(nameof(Suggestions));
}

public AutoSuggestTextBoxWithCollectionViewViewModel()
{
BaseSuggestions = new()
{
BaseSuggestions =
[
"Apples",
"Bananas",
"Beans",
"Mtn Dew",
"Orange",
};
];
Suggestions = CollectionViewSource.GetDefaultView(BaseSuggestions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
d:DataContext="{d:DesignInstance Type=local:AutoSuggestTextBoxWithTemplateViewModel}"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<!-- TODO SELECTED ITEM -->
<materialDesign:AutoSuggestBox
materialDesign:HintAssist.HelperText="Name"
materialDesign:TextFieldAssist.HasClearButton="True"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public partial class AutoSuggestTextBoxWithTemplateViewModel : ObservableObject
private List<SuggestionThing> BaseSuggestions { get; }

[ObservableProperty]
private ObservableCollection<SuggestionThing> _suggestions = new();
private ObservableCollection<SuggestionThing> _suggestions = [];

[ObservableProperty]
private string? _autoSuggestText;
Expand All @@ -41,14 +41,14 @@ partial void OnAutoSuggestTextChanged(string? oldValue, string? newValue)

public AutoSuggestTextBoxWithTemplateViewModel()
{
BaseSuggestions = new()
{
BaseSuggestions =
[
new("Apples"),
new("Bananas"),
new("Beans"),
new("Mtn Dew"),
new("Orange"),
};
];
Suggestions = new ObservableCollection<SuggestionThing>(BaseSuggestions);
}

Expand All @@ -62,9 +62,7 @@ private static bool IsMatch(string item, string currentText)
}
}

public class SuggestionThing
public class SuggestionThing(string name)
{
public string Name { get; }

public SuggestionThing(string name) => Name = name;
public string Name { get; } = name;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections;
using System.ComponentModel;
using MaterialDesignThemes.UITests.Samples.AutoSuggestBoxes;
using MaterialDesignThemes.UITests.Samples.AutoSuggestTextBoxes;
Expand Down Expand Up @@ -201,6 +200,38 @@ public async Task AutoSuggestBox_KeysUpAndDown_WrapAround()
Assert.Equal(0, await suggestionListBox.GetSelectedIndex());
}

[Fact]
[Description("Issue 3845")]
public async Task AutoSuggestBox_SelectingAnItem_SetsSelectedItem()
{
await using var recorder = new TestRecorder(App);

//Arrange
IVisualElement userControl = await LoadUserControl<AutoSuggestTextBoxWithCollectionView>();
IVisualElement<AutoSuggestBox> suggestBox = await userControl.GetElement<AutoSuggestBox>();
IVisualElement<Popup> popup = await suggestBox.GetElement<Popup>();
IVisualElement<ListBox> suggestionListBox = await popup.GetElement<ListBox>();

//Act
await suggestBox.MoveKeyboardFocus();
await Task.Delay(50);
await suggestBox.SendKeyboardInput($"B{Key.Down}{Key.Enter}");
await Task.Delay(50);

//Assert
string? selectedItem = (await suggestBox.GetSelectedItem()) as string;
Assert.Equal("Bananas", selectedItem);

static void AssertViewModelProperty(AutoSuggestBox autoSuggestBox)
{
var viewModel = (AutoSuggestTextBoxWithCollectionViewViewModel)autoSuggestBox.DataContext;
Assert.Equal("Bananas", viewModel.SelectedItem);
}
await suggestBox.RemoteExecute(AssertViewModelProperty);

recorder.Success();
}

private static async Task AssertExists(IVisualElement<ListBox> suggestionListBox, string text, bool existsOrNotCheck = true)
{
try
Expand Down
Loading