Skip to content

Commit 7246378

Browse files
committed
Set SelectedItemProperty when value is commit and bind it 2-way by default
1 parent 806146b commit 7246378

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
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/WPF/AutoSuggestBoxes/AutoSuggestTextBoxTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,33 @@ public async Task AutoSuggestBox_KeysUpAndDown_WrapAround()
201201
Assert.Equal(0, await suggestionListBox.GetSelectedIndex());
202202
}
203203

204+
[Fact]
205+
[Description("Issue 3845")]
206+
public async Task AutoSuggestBox_SelectingAnItem_SetsSelectedItem()
207+
{
208+
await using var recorder = new TestRecorder(App);
209+
210+
//Arrange
211+
IVisualElement<AutoSuggestBox> suggestBox = (await LoadUserControl<AutoSuggestTextBoxWithTemplate>()).As<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.SendInput(new KeyboardInput("B"));
219+
await Task.Delay(50);
220+
await suggestBox.SendInput(new KeyboardInput(Key.Enter));
221+
await Task.Delay(50);
222+
223+
//Assert
224+
string? selectedItem = (await suggestBox.GetSelectedItem()) as string;
225+
Assert.NotNull(selectedItem);
226+
Assert.Equal("Bananas", selectedItem);
227+
228+
recorder.Success();
229+
}
230+
204231
private static async Task AssertExists(IVisualElement<ListBox> suggestionListBox, string text, bool existsOrNotCheck = true)
205232
{
206233
try

0 commit comments

Comments
 (0)