Skip to content
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

Changing combox box items on open/close broken #4892

Closed
grokys opened this issue Oct 20, 2020 · 1 comment
Closed

Changing combox box items on open/close broken #4892

grokys opened this issue Oct 20, 2020 · 1 comment
Labels

Comments

@grokys
Copy link
Member

grokys commented Oct 20, 2020

This seems to have got broken by #4855: adding combo box items when the dropdown is opened now causes the dropdown to be measured incorrectly:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
        x:Class="Sandbox.MainWindow">
    <ComboBox Name="cb" Items="{Binding Items}" SelectedIndex="{Binding SelectedIndex}" VirtualizationMode="Simple"/>
</Window>
public class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.AttachDevTools();

            var vm = new ViewModel();
            var cb = this.FindControl<ComboBox>("cb");

            cb.GetObservable(ComboBox.IsDropDownOpenProperty).Skip(1).Subscribe(x =>
            {
                if (x)
                {
                    vm.DropDownOpened();
                }
                else
                {
                    vm.DropDownClosed();
                }
            });

            DataContext = vm;
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }
    }

    public class ViewModel : ReactiveObject
    {
        private int _selectedIndex = 0;

        public ObservableCollection<string> Items { get; } = new ObservableCollection<string> { "foo" };

        public int SelectedIndex
        {
            get => _selectedIndex;
            set => this.RaiseAndSetIfChanged(ref _selectedIndex, value);
        }

        public void DropDownOpened()
        {
            var items = new[] { "foo", "bar", "baz" };
            Items.Clear();
            foreach (var i in items)
            {
                Items.Add(i);
            }
            SelectedIndex = 1;
        }

        public void DropDownClosed()
        {
            Items.Clear();
            Items.Add("bar");
            SelectedIndex = 0;
        }
    }

cc: @donandren

@grokys
Copy link
Member Author

grokys commented Oct 21, 2020

Fixed by #4895

@grokys grokys closed this as completed Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant