AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.1k stars 2.26k forks source link

Combobox selected item value set null in datagrid when the tab change #12811

Open Nikhilthambi opened 1 year ago

Nikhilthambi commented 1 year ago

Describe the bug I have a Datagrid and inside this Datagrid there is one Combobox. When I switch the tab the Combobox SelectedItem is empty automatically without any action.

To Reproduce Steps to reproduce the behavior:

  1. Create 2 tabs
  2. Inside one tab create a Datagrid and fill the cells one cell should be Combobox with list and selectedItem
  3. Switch the tab and come back to Datagrid tab
  4. You can see the selectedItem will be empty

Expected behavior To have the combobox retain their values after switching tabs (When change the Tab somehow the SelectedItem property value is set to null. This should not happen)

Screenshots Before switching tabs: image

After switching tab: image

Desktop (please complete the following information):

Additional context there is already a closed ticket same scenario like this but the issue is still there. Please find the related closed bug ticket: https://github.com/AvaloniaUI/Avalonia/issues/5661

Could you please check and fix this issue?

Thanks

timunie commented 1 year ago

is the ComboBox bound to anything in the ViewModel?

Nikhilthambi commented 1 year ago

yes the value bound in the viewmodel.

timunie commented 1 year ago

In that case that shouldn't happen. However, current supported version is 11.0.4, so unlikely a bug in v0.10 gets fixed. I see two options for now:

  1. Enable compiled bindings (if not yet) to debug binding issues better. If that doesn't help,
  2. try DataGridTemplateColumn
ykarpeev commented 1 year ago

I am seeing an issue that I think is related. Selected item in combobox is null - if I scroll in my datagrid or resize the window then the combox selected value value shows up. This is with 11.0.4.

image

and after manipulating window size a few times.

image

I'll try to look into it here to see if I can find anything.

timunie commented 1 year ago

@ykarpeev please file a minimal sample to reproduce.

Nikhilthambi commented 1 year ago

I am seeing an issue that I think is related. Selected item in combobox is null - if I scroll in my datagrid or resize the window then the combox selected value value shows up. This is with 11.0.4.

image

and after manipulating window size a few times.

image

I'll try to look into it here to see if I can find anything.

I am also getting something similar issue, my case when I switch the tab and I tried with this 11.0.2 version.

Nikhilthambi commented 1 year ago

In that case that shouldn't happen. However, current supported version is 11.0.4, so unlikely a bug in v0.10 gets fixed. I see two options for now:

  1. Enable compiled bindings (if not yet) to debug binding issues better. If that doesn't help,
  2. try DataGridTemplateColumn

I tried both solution but still getting the same issue

timunie commented 1 year ago

@Nikhilthambi if you have a minimal sample to reproduce, please share it.

ykarpeev commented 1 year ago

Here is a minimal sample.

https://github.com/ykarpeev/avaloniadatagridrepro

For me, on resizing the window to a few pixels wide then resizing back a couple of times will make some of the selected items show up.

ykarpeev commented 1 year ago

https://github.com/AvaloniaUI/Avalonia/assets/6986975/92dbca59-f018-4dbd-bb43-49966e9a9375

timunie commented 1 year ago

@ykarpeev found your issue. You are binding to $parent[] but DataGridColumn cannot resolve it. So better to bind to named element here. Set x:Name to the window or DataGrid and bind to it via #root.DataContext...

Sent a PR: https://github.com/ykarpeev/avaloniadatagridrepro/pull/1

ykarpeev commented 1 year ago

Thank you. That solves part of my issue, but I think mine must be separate from the parent and is related to the fact that my datagrid is in a user control. If they are using a usercontrol maybe it is still the same.

Switching between tabs I see new errors

Binding]Error in binding to 'datagridrepro.Views.UserControl1'.'DataContext': 'Null value in expression '{empty}' at ''.'(UserControl1 #33189039) [Binding]Error in binding to 'Avalonia.Controls.ComboBox'.'ItemsSource': 'Null value in expression '{empty}' at '$parent[UserControl, 0].DataContext'.'(ComboBox #50685404) [Binding]Error in binding to 'Avalonia.Controls.ComboBox'.'ItemsSource': 'Null value in expression '{empty}' at '$parent[UserControl, 0].DataContext'.'(ComboBox #41270904) [Binding]Error in binding to 'Avalonia.Controls.ComboBox'.'ItemsSource': 'Null value in expression '{empty}' at '$parent[UserControl, 0].DataContext'.'(ComboBox #51461341)

https://github.com/AvaloniaUI/Avalonia/assets/6986975/178aee8a-ce9c-4ead-881a-c1826b129013

ykarpeev commented 1 year ago

I fixed this by wrapping the usercontrol in a contentcontrol..

        _<ContentControl>
            <local:UserControl1 DataContext="{Binding}" />
        </ContentControl>_
timunie commented 1 year ago

@ykarpeev you can also give the DataGrid or UserControl or whatever a name and use the same strategy.