Replies: 5 comments 6 replies
-
Same issue here, any idea yet? |
Beta Was this translation helpful? Give feedback.
-
I'm seeing the same issue when I use ListView.ItemTemplate. I see a Diagnostics.BindingDiagnostics: Warning for each item indicating the property is not found on the page's ViewModel. I'm binding the ItemSource to the view model's Items property and have tried setting x:DataType on the element, the element and, lastly, on the child element under the ViewCell. I get the warning regardless of which element I set in the DataTemplate. I've also tried removing the x:DataType from all elements in the page with no success. From the behavior, it appears that x:DataType is completely ignored on items added via ItemsSource. I have three cases where I'm using ListView with ItemsSource. In one, I use a direct DataTemplate for the ItemTemplate. In the two others, I use a DataTemplateSelector. In all cases, I see this warning. The problem is exacerbated in one of the usages because the items are also collections and the DataTemplate uses a BindableLayout to render them. In that case, I get the warning as well as an InvalidCastException attempting to cast the viewmodel to an IEnumerable instead of the contained item. Since the bindings end up working at runtime, it appears to be failing and then succeeding (i.e., it's doing twice the work). |
Beta Was this translation helpful? Give feedback.
-
I've dug into this a little more and found the culprit: The problem occurs when the calling Element.SetParent on the ViewCell. This calls BindableObject.SetInheritedBindingContext which ends up setting the ListView's BindingContext on the ViewCell instead of the intended value from the ItemsSource. This propogates down the the StackLayout's ItemsSource which expects an IEnumerable. Since the ListView's BindingContext is not an IEnumerable, an InvalidCastException(Object must implement IConvertible) is raised by Convert.ChangeType. If the ListView's BindingContext does implement IEnumerable, a flood of BindingDiagnostics: Warning occur for each Cell also due to the incorrect value. I see this sequence occur twice for each cell as the ListView is populated causing a user noticeable delay in displaying the associated page. To me, this is clearly a bug. If an ItemsSource is defined on the ListView, the cell should be getting the associated object in the ItemsSource for its BindingContext, NOT the parent's BindingContext. |
Beta Was this translation helpful? Give feedback.
-
This issue appears to have worked it way to the right people at .NET MAUI central. I no longer experience these binding issues. Most likely a bug that was resolved, although I haven't confirmed. |
Beta Was this translation helpful? Give feedback.
-
Workaround to prevent the warnings (and InvalidCastExceptions):
|
Beta Was this translation helpful? Give feedback.
-
Hi all,
In the process wrapping my head around everything, but I found something strange. I am not sure if it is me or a possible bug.
I use the MauiAppBuilder to inject my views and viewmodels for a navigation service I have.
I also set the BindingContext property on the view's codebehind to the viewmodel I injected, which is presented to me via the view's constructor. The build in DI provided by maui is great, btw!
Because I do not set the binding context in the xaml declarations, the editor shows that it can't find the specified property, which is expected at design time. It works fine for normal properties at run time, and does not show any binding failures. Great!
However, I have an ObservableCollection property that are bound to a listview's ItemSource, and within the DataTemplate of the listview, I individually bind to the relevant properties of the type in the collection.
When I run the app, it shows binding errors in the XAML Bind Failures window, however everything is bound and is displaying correctly in the listview!?
I do not mind seeing the errors, provided that it works, but I am concerned that something might bite me later that I am not aware of.
Or could it be a false positive by the framework due to the way I inject and use the views/models?
Beta Was this translation helpful? Give feedback.
All reactions