Invalid AXAML file compiles but gives misleading runtime errors #21130
astroman133
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have been troubleshooting an issue for several hours. I decided to re-create the issue in a demo app and in the process I was able to see and correct the issue.
The symptoms are that the demo app compiles successfully, but at runtime gives the following errors:
[Binding]An error occurred binding 'ItemsSource' to 'People' at 'People': 'Items collection must be empty before using ItemsSource.' (ListBox #45083904)
[Binding]An error occurred binding 'ItemsSource' to 'People' at 'People': 'Items collection must be empty before using ItemsSource.' (ListBox #45083904)
Exception thrown: 'System.InvalidCastException' in System.Private.CoreLib.dll
[Binding]An error occurred binding 'Text' to 'FirstName' at 'FirstName': 'Unable to cast object of type 'System.String' to type 'TestListboxApp.Models.Person'.' (TextBlock #32167247)
Exception thrown: 'System.InvalidCastException' in System.Private.CoreLib.dll
[Binding]An error occurred binding 'Text' to 'LastName' at 'LastName': 'Unable to cast object of type 'System.String' to type 'TestListboxApp.Models.Person'.' (TextBlock #15521030)
I am using MVVM and binding to the ItemsSource property of the ListBox. I am not using the Items property at all. Also, the reported binding errors on the FirstName and LastName properties are misleading.
I was able to resolve the issue by deleting an errant character in the MainWindow.axaml file. It is on line 37 after the closing ItemTemplate tag.
The minimal project that re-creates the issue is attached. It was created with Visual Studio 2026 using Avalonia 11.3.11
TestListboxApp.zip
Beta Was this translation helpful? Give feedback.
All reactions