Skip to content

Commit 3b30b20

Browse files
corvinszKeboo
andauthored
Fix AutoSuggestBox not setting SelectedItem (#3847)
* Set SelectedItemProperty when value is commit and bind it 2-way by default * Updated UI tests to better asserts on the desired behavior Updated some of the test examples to latest C# syntax. --------- Co-authored-by: Kevin Bost <[email protected]>
1 parent f5a504d commit 3b30b20

File tree

6 files changed

+56
-18
lines changed

6 files changed

+56
-18
lines changed

src/MaterialDesignThemes.Wpf/AutoSuggestBox.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ public object SelectedItem
9696
set => SetValue(SelectedItemProperty, value);
9797
}
9898
public static readonly DependencyProperty SelectedItemProperty =
99-
DependencyProperty.Register(nameof(SelectedItem), typeof(object), typeof(AutoSuggestBox), new PropertyMetadata(default(object)));
99+
DependencyProperty.Register(
100+
nameof(SelectedItem),
101+
typeof(object),
102+
typeof(AutoSuggestBox),
103+
new FrameworkPropertyMetadata(default(object), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
100104

101105

102106
public object SelectedValue
@@ -254,6 +258,7 @@ private bool CommitValueSelection(object? selectedValue)
254258
{
255259
CaretIndex = Text.Length;
256260
}
261+
SetCurrentValue(SelectedItemProperty, selectedValue);
257262
CloseAutoSuggestionPopUp();
258263
var args = new RoutedPropertyChangedEventArgs<object?>(oldValue, Text)
259264
{

tests/MaterialDesignThemes.UITests/Samples/AutoSuggestBoxes/AutoSuggestTextBoxWithCollectionView.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
materialDesign:HintAssist.HelperText="Name"
1515
materialDesign:TextFieldAssist.HasClearButton="True"
1616
Text="{Binding AutoSuggestText, UpdateSourceTrigger=PropertyChanged}"
17-
Suggestions="{Binding Suggestions}">
17+
Suggestions="{Binding Suggestions}"
18+
SelectedItem="{Binding SelectedItem}"
19+
>
1820
</materialDesign:AutoSuggestBox>
1921
<!--
2022
<ComboBox ItemsSource="{Binding Suggestions}"

tests/MaterialDesignThemes.UITests/Samples/AutoSuggestBoxes/AutoSuggestTextBoxWithCollectionView.xaml.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using CommunityToolkit.Mvvm.ComponentModel;
2-
using System.Collections.ObjectModel;
1+
using System.Collections.ObjectModel;
32
using System.ComponentModel;
43
using System.Windows.Data;
4+
using CommunityToolkit.Mvvm.ComponentModel;
55

66
namespace MaterialDesignThemes.UITests.Samples.AutoSuggestBoxes;
77

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

29+
[ObservableProperty]
30+
private string? _selectedItem;
31+
2932
partial void OnAutoSuggestTextChanged(string? oldValue, string? newValue)
3033
{
3134
if (!string.IsNullOrWhiteSpace(newValue))
@@ -36,19 +39,19 @@ partial void OnAutoSuggestTextChanged(string? oldValue, string? newValue)
3639
{
3740
Suggestions.Filter = null;
3841
}
39-
base.OnPropertyChanged(nameof(Suggestions));
42+
OnPropertyChanged(nameof(Suggestions));
4043
}
4144

4245
public AutoSuggestTextBoxWithCollectionViewViewModel()
4346
{
44-
BaseSuggestions = new()
45-
{
47+
BaseSuggestions =
48+
[
4649
"Apples",
4750
"Bananas",
4851
"Beans",
4952
"Mtn Dew",
5053
"Orange",
51-
};
54+
];
5255
Suggestions = CollectionViewSource.GetDefaultView(BaseSuggestions);
5356
}
5457

tests/MaterialDesignThemes.UITests/Samples/AutoSuggestBoxes/AutoSuggestTextBoxWithTemplate.xaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
d:DataContext="{d:DesignInstance Type=local:AutoSuggestTextBoxWithTemplateViewModel}"
99
mc:Ignorable="d"
1010
d:DesignHeight="450" d:DesignWidth="800">
11-
<!-- TODO SELECTED ITEM -->
1211
<materialDesign:AutoSuggestBox
1312
materialDesign:HintAssist.HelperText="Name"
1413
materialDesign:TextFieldAssist.HasClearButton="True"

tests/MaterialDesignThemes.UITests/Samples/AutoSuggestBoxes/AutoSuggestTextBoxWithTemplate.xaml.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public partial class AutoSuggestTextBoxWithTemplateViewModel : ObservableObject
2121
private List<SuggestionThing> BaseSuggestions { get; }
2222

2323
[ObservableProperty]
24-
private ObservableCollection<SuggestionThing> _suggestions = new();
24+
private ObservableCollection<SuggestionThing> _suggestions = [];
2525

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

4242
public AutoSuggestTextBoxWithTemplateViewModel()
4343
{
44-
BaseSuggestions = new()
45-
{
44+
BaseSuggestions =
45+
[
4646
new("Apples"),
4747
new("Bananas"),
4848
new("Beans"),
4949
new("Mtn Dew"),
5050
new("Orange"),
51-
};
51+
];
5252
Suggestions = new ObservableCollection<SuggestionThing>(BaseSuggestions);
5353
}
5454

@@ -62,9 +62,7 @@ private static bool IsMatch(string item, string currentText)
6262
}
6363
}
6464

65-
public class SuggestionThing
65+
public class SuggestionThing(string name)
6666
{
67-
public string Name { get; }
68-
69-
public SuggestionThing(string name) => Name = name;
67+
public string Name { get; } = name;
7068
}

tests/MaterialDesignThemes.UITests/WPF/AutoSuggestBoxes/AutoSuggestTextBoxTests.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections;
21
using System.ComponentModel;
32
using MaterialDesignThemes.UITests.Samples.AutoSuggestBoxes;
43
using MaterialDesignThemes.UITests.Samples.AutoSuggestTextBoxes;
@@ -201,6 +200,38 @@ public async Task AutoSuggestBox_KeysUpAndDown_WrapAround()
201200
Assert.Equal(0, await suggestionListBox.GetSelectedIndex());
202201
}
203202

203+
[Fact]
204+
[Description("Issue 3845")]
205+
public async Task AutoSuggestBox_SelectingAnItem_SetsSelectedItem()
206+
{
207+
await using var recorder = new TestRecorder(App);
208+
209+
//Arrange
210+
IVisualElement userControl = await LoadUserControl<AutoSuggestTextBoxWithCollectionView>();
211+
IVisualElement<AutoSuggestBox> suggestBox = await userControl.GetElement<AutoSuggestBox>();
212+
IVisualElement<Popup> popup = await suggestBox.GetElement<Popup>();
213+
IVisualElement<ListBox> suggestionListBox = await popup.GetElement<ListBox>();
214+
215+
//Act
216+
await suggestBox.MoveKeyboardFocus();
217+
await Task.Delay(50);
218+
await suggestBox.SendKeyboardInput($"B{Key.Down}{Key.Enter}");
219+
await Task.Delay(50);
220+
221+
//Assert
222+
string? selectedItem = (await suggestBox.GetSelectedItem()) as string;
223+
Assert.Equal("Bananas", selectedItem);
224+
225+
static void AssertViewModelProperty(AutoSuggestBox autoSuggestBox)
226+
{
227+
var viewModel = (AutoSuggestTextBoxWithCollectionViewViewModel)autoSuggestBox.DataContext;
228+
Assert.Equal("Bananas", viewModel.SelectedItem);
229+
}
230+
await suggestBox.RemoteExecute(AssertViewModelProperty);
231+
232+
recorder.Success();
233+
}
234+
204235
private static async Task AssertExists(IVisualElement<ListBox> suggestionListBox, string text, bool existsOrNotCheck = true)
205236
{
206237
try

0 commit comments

Comments
 (0)