question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get old selected value instead of new selected value from comboBox in MVVM application

See original GitHub issue

Describe 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

image the application startup

image step 2

image 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:open
  • Created 5 months ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
timuniecommented, Aug 1, 2023

Otherwise you will need to wait for someone to have a time for it to do.

0reactions
timuniecommented, Aug 1, 2023

No a fix if you are able to fix it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WPF ComboBox SelectedItem - change to previous value
When the user says "no", WPF is unaware that the value has changed. As far as WPF is concerned, the value is whatever...
Read more >
How to retrieve value for previously selected item from the ...
To retrieve the current value in a SelectedIndexChanged or SelectedValueChanged event handler, use the SelectedItem property instead. The good ...
Read more >
Need to use SelectedItem instead of SelectedValue in a ...
I found a way of getting around it. Instead of using SelectedValue, I'm going to use SelectedIndex. That way I can define a...
Read more >
How to store ComboBox selected Item in a Variable in WPF ...
Hi, I want to store ComboBox selected Item in a Variable so, that I can call or retrieve the variable later according to...
Read more >
Explain Combo Box Binding In MVVM - WPF
Now, for binding the combo box in WPF MVVM, I will explain two methods -- one is using simple binding and another is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found