Get old selected value instead of new selected value from comboBox in MVVM application
See original GitHub issueDescribe the bug
I use avalonia to build a MVVM application, using a comboBox to select items. I set selectedItem with viewmoel binding ,set a reactivecommand for handle SelectionChanged event and bind this event. But when i select other item from combobox , in command for handle event , i got the old selected value instead of the new selected value. I write a exmaple application to show the bug. Here are view code and viewmodel code .
To Reproduce
Steps to reproduce the behavior:
1.Clone the code , build and run the example application 2.Select any item in comboBox , for example First , you can see the textBlock’s text is "Current Selected " 3.Select another item in comboBox ,for example Second you can see the textBlock’s text is “Current Selected First”
the setp 2 and 3 can reproduce the behavior , because the event for command get the old selected value rather than new selected value.
Expected behavior
I can get the new selected value when SelectionChanged event is raised.
Screenshots
the application startup
step 2
step 3
Desktop (please complete the following information):
- OS: Windows 10 Perfessional 21H2
- Version 0.10.19
Additional context
I try to debug it , and i found afrer the selectedItem changed , the control raise the SelectionChanged event first the update the selectedItem to new value. I guess the occurrence of bugs is related to the order of execution. But i couldn’t write a correct test in Avalonia.Controls.UnitTests to prove the bug is exist and code change is right. I have no idea to write a binding code equal to axaml binding like this in test . This is the unfinished test code I wrote
[Fact]
public void Binding_SelectionChanged_Event()
{
var inputManagerMock = new Moq.Mock<IInputManager>();
var services = TestServices.StyledWindow.With(inputManager: inputManagerMock.Object, keyboardDevice: () => new KeyboardDevice());
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var window = new Window();
var target = new ComboBox()
{
Items = { "First", "Second", "Third", "Fourth" },
SelectedIndex = -1,
Template = GetTemplate(),
};
string recordText = null;
string bindText = null;//the variable need to bind
target.SelectionChanged += (sender, args) =>
{
};
window.Content = target;
target.Focus();
target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Down,
});
target.RaiseEvent(new KeyEventArgs
{
RoutedEvent = InputElement.KeyDownEvent,
Key = Key.Down,
});
Assert.True(recordText == target.SelectionBoxItem.ToString());
}
}
Issue Analytics
- State:
- Created 5 months ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
Otherwise you will need to wait for someone to have a time for it to do.
No a fix if you are able to fix it.