lepoco / wpfui

WPF UI provides the Fluent experience in your known and loved WPF framework. Intuitive design, themes, navigation and new immersive controls. All natively and effortlessly.
https://wpfui.lepo.co
MIT License
7.03k stars 666 forks source link

ArgumentNullException when removing items from ObservableCollection bound to ItemsControl with CardExpander items #1140

Open bujma opened 2 weeks ago

bujma commented 2 weeks ago

Describe the bug

System.ArgumentNullException: 'Value cannot be null. Parameter name: defaultDestinationValue' when removing items of ObserableCollection<Model> bound from ViewModel class to ItemsControl in Window with ItemTemplate templated with CardExpander after expanding any CardExpander.

To Reproduce

Situation:

Steps:

  1. In runtime, fill the Items with some Model items (button [Reload Items] calling Items = new ObservableCollection<Model>(list)).
  2. Expand any CardExpander.
  3. Remove any item/all items/assign new instance of ObservableCollection to Items property (buttons [Delete], [Remove All Items] or [Reload Items] calling Items.Remove(item), Items.Clear() or code from step 1).
    => exception will be thrown

Description:

ErrorProof.zip

Expected behavior

No exception thrown. Removing items from ObservableCollection or assigning new instance should have no such impact.

Screenshots

No response

OS version

Windows 10 Pro 22H2 19045.4529

.NET version

4.7.2

WPF-UI NuGet version

3.0.4

Additional context

No response

textGamex commented 1 week ago

Thank you for providing the complete sample code; I think this will be helpful in fixing this bug.

textGamex commented 1 week ago

I observed that you were utilizing the ResourceDictionary within the MainWindow; however, when I configure the ResourceDictionary in the App.xaml, this exception does not occur.

bujma commented 1 week ago

Is it unsupported in WPF to put style resources into Window? Or is putting the resources into App.xaml the only way to have it working?

The situation is: all our WPF windows/user controls are in libraries. The libraries are used from win32 (non .NET) code => there is no Application object created. I know, I can create own one in the library and merge the WPF-UI resources into it - I already do it with our current WPF styles that we want to replace with WPF-UI. But if I do it this way, all windows are affected by these styles at once and we want to restyle them one by one and to not wait with the release until all of them are restyled - it's OK for us to have some with our old style and some with WPF-UI's (our one is close to WPF-UI, so not a big "disharmony").
I already have bad experience with this "general styles" approach: when an application loads our library with its styles automatically merged into Application's resources, all styles of the application itself and of other libraries are affected. Therefore I decided to put WPF-UI resources directly to windows/user controls that are being restyled.

Is there any better solution I am not aware of?

Thanks much for your help.

textGamex commented 1 week ago

Is it unsupported in WPF to put style resources into Window? Or is putting the resources into App.xaml the only way to have it working?

The situation is: all our WPF windows/user controls are in libraries. The libraries are used from win32 (non .NET) code => there is no Application object created. I know, I can create own one in the library and merge the WPF-UI resources into it - I already do it with our current WPF styles that we want to replace with WPF-UI. But if I do it this way, all windows are affected by these styles at once and we want to restyle them one by one and to not wait with the release until all of them are restyled - it's OK for us to have some with our old style and some with WPF-UI's (our one is close to WPF-UI, so not a big "disharmony"). I already have bad experience with this "general styles" approach: when an application loads our library with its styles automatically merged into Application's resources, all styles of the application itself and of other libraries are affected. Therefore I decided to put WPF-UI resources directly to windows/user controls that are being restyled.

Is there any better solution I am not aware of?

Thanks much for your help.

Sorry, I'm not quite knowledgeable about this aspect either.

bujma commented 1 week ago

Thanks for your response. I was trying the same with some other styles placed directly in Window's resources - as is perfectly common in WPF - and everything worked. How can this unexpected behaviour of WPF-UI resources be fixed?
Thanks for the time of you all.