Closed grzegorz-wolszczak closed 19 hours ago
for reference I've also created this in Jetbrains Rider bug tracker: https://youtrack.jetbrains.com/issue/RIDER-120607/InvalidCastException-in-when-debugging-Avalonia-application-.NET-8.0
I think you need to set the DataType
on your DataTemplate
to {x:Type vm:UserDetailsControl}
so that the compiled bindings know what the properties in the template relate to.
@halfninja Would this explain why Rider in debug mode throws exception ? Why then Visual Studio in debug mode does not throw , and Rider in release mode does not throw... ?
I'm not sure, that is unusual. One thing I did note when debugging in Rider is that if you click continue past these exception breaks, the app displays and appears to work okay.
I tried running your project and setting the data type didn't help (I think it might already infer it from the type of ItemsSource
)- but I hadn't seen the use of the Parent
property like this before. It seems happier instead binding the DataContext
like this
<vm:UserDetailsControl DataContext="{Binding #userList.SelectedItem, Mode=OneWay}" />
It looks like there's no need for UserList_OnSelectionChanged, as this binding keeps it up to date on its own.
This is happening when a collection element attached to visual tree and inherit parent DataContext by default. Not a big problem after children DataContext is assigned.
What happens if you set DataContext before I calling Initialize? I believe that this may be the related since the compoler don't know about the data context during construction of the UI
Edit:
- <vm:UserDetailsControl Name="CustomControl" Parent="{Binding #userList.SelectedItem}" />
+ <vm:UserDetailsControl Name="CustomControl" DataContext="{Binding #userList.((vm:DetailsViewModel)SelectedItem)}" />
@timunie
<!-- <vm:UserDetailsControl Name="CustomControl" Parent="{Binding #userList.SelectedItem}" /> -->
<vm:UserDetailsControl Name="CustomControl" DataContext="{Binding #userList.((vm:UserDetailsViewModel)SelectedItem)}" />
has helped (!!) and the issue does not appear.
Knowing what we know now, can we answer the question: where
is the problem ? Is in in Avalonia code or in Rider ? If in Rider than I'd like to close this issue here.
The strange thing is that this issue does not happen e.g. in VisualStudio during debugging. (WHY ?! )
Thinking further I think the issue is a missuse of Parent and it's not supposed to work at all. So imo both issues can be closed. Even if it doesn't crash, you shouldn't change parent of a control. Better to bind to DataContext. Bonus, you can remove the event handler for SelectionChanged.
Describe the bug
Not sure if it is related to Avalonia or Jetbrains Raider or somewhere 'in between'
I have simple applicaton whith one listbox and one custom control. When you click on listbox element , the same content should be displayed in custom control
Code
my view models:
main window code
main window axaml
user control code
user control axaml
When I try to run this application in debug mode in Jebrains rider
I've provided example code to reproduce.
Verified that: this issue is not present when debugging in VisualStudio
To Reproduce
example_issue_code.zip
Expected behavior
No exceptions should be visible
Avalonia version
11.2.2
OS
Windows
Additional context
My setup: I'm using CommunityToolkit.Mvvm 8.3.2 Jetbrains Rider 2024.3 (Build #RD-243.21565.191) application target framework is
<TargetFramework>net8.0</TargetFramework>
mydotnet --info output