maui: XAML bindings are flagging invalid binding failures in ListView - it is confused as to what the data context is...

Description

I have a list view which has its items source set to an observable collection in a top-level view model. The collection is a list of detail view models. I have explicitly set the data context in the item template to point to the detail view model, yet the error it flags indicates it is looking at the page’s data context and not the context of a single element in the list. I hate to see any binding failures, but this one will not go away…

Here is the binding error listed (there are three, actually):

Data Context	EmailsViewModel
Binding Path	Email
Target		Label.Text
Target Type	String
Description	'Email' property not found on 'MobyClient.ViewModels.EmailsViewModel', target property: 'Microsoft.Maui.Controls.Label.Text'
File			\Pages\PageEmails.xaml
Line			268

Here is the EmailsViewModel class:

namespace MobyClient.ViewModels;

public partial class EmailsViewModel : ObservableObject {

	public EmailsViewModel (IMobyLogger logger, EmailService emailService) {

		Guard.IsNotNull (logger);
		Guard.IsNotNull (emailService);

		_logger = logger;
		_emailService = emailService;
	}

	public ObservableCollection<EmailViewModel> FilteredEmails { get; set; }
	public ObservableCollection<FolderViewModel> Folders { get; set; }

	private void InitializeViewModel () {
		if (_isInitialized) return;

		_filteredEmails = new List<Email> (_emailService.Emails);

		FilteredEmails = new ObservableCollection<EmailViewModel> ();
		Folders = new ObservableCollection<FolderViewModel> (_emailService.Folders);

		_logger.LogDebug ("Emails viewmodel is initialized.");

		_isInitialized = true;
	}

	private bool _isInitialized;
	private List<Email> _filteredEmails;
	private readonly IMobyLogger _logger;
	private readonly EmailService _emailService;
}

And here is the EmailViewModel:

namespace MobyClient.ViewModels;

public partial class EmailViewModel : ObservableObject {

	[ObservableProperty]
	private Email _email;
}

Looking at the xaml used to define the list view item template:

<ListView.ItemTemplate>
	<DataTemplate>
		<ViewCell>
			<Grid
				Margin="0,1,0,0"
				Padding="3,0">
				<Grid
					x:Name="FolderItemTemplateGrid"
					x:DataType="viewModels:EmailViewModel"
					ColumnDefinitions="Auto, *, Auto"
					ColumnSpacing="5"
					Margin="10,2"
					ZIndex="1000">
					<Label
						Grid.Column="0"
						FontFamily="SegoeUiRegular"
						FontSize="14"
						Text="{Binding Email.SenderEmail.ShortDisplayName}" />
					<Label
						Grid.Column="1"
						FontFamily="SegoeUiRegular"
						FontSize="14"
						Margin="10,0,0,0"
						MaximumWidthRequest="1000"
						Text="{Binding Email.Subject}" />
					<Label
						Grid.Column="2"
						FontFamily="SegoeUiRegular"
						FontSize="14"
						Margin="10,0,0,0"
						Text="{Binding Email.MessageDate}" />
				</Grid>
			</Grid>
		</ViewCell>
	</DataTemplate>
</ListView.ItemTemplate>

I am clearly setting the datatype for the item template to be of the EMAIL view model and NOT the EMAILS view model! Trust me when I say that the Email class DOES have the properties, SenderEmail.ShortName (from an EmailAddress class), Email.Subject, and Email.Message date.

So, why is the error pointing to the EmailsViewModel when I have set the datatype to be EmailViewModel? Intellisense works (which to my understanding is driven by the x:DataType property… I don’t get it!

My code base has grown to a size where it is not feasible to give you a github repository, so the code snippets above will have to work.

Steps to Reproduce

I know you won’t like this, but you will need to generate a simple app that has a listview with an itemtemplate defined as I listed above.

I can see no reason why these binding errors are being flagged…

Version with bug

6.0.400 (current)

Last version that worked well

Unknown/Other

Affected platforms

Windows, I was not able test on other platforms

Affected platform versions

net6.0-windows10.0.19041.0

Did you find any workaround?

No…

Relevant log output

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 4
  • Comments: 31 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I think I’m seeing the same issue, here’s my repro project: https://github.com/spadapet/maui_bugs

Build and run the “BadBindingFailure” project in that solution.

Click between the Test X tabs and it creates unnecessary binding failures which you can see in the VS binding failures window. The wrong data context is temporarily used:

image

image

Same here, had to switch to a CollectionView to stop the false binding errors. Just want to be sure that I’m using the most optimized approach to displaying simple list items.

I’m also having the same issue and switching to a CollectionView fixed it.

I get the same errors,

[14:03:18 WRN] 'TestString' property not found on 'MyApp.ViewModels.TestViewModel', target property: 'UraniumUI.Material.Controls.TextField.Text'

and I have A LOT of properties on the page. I can reproduce that the error is generated depending on whether it is a read-only property.

I.e., the warning will be generated if the property has only a getter and no setter.

<Entry Text="{Binding TestString}" />
public string TestString => "Test"; // (erroneous) warning
public string TestString { get; set; } = "Test" // no warning

Like andrewhenry3, I experienced the same issue as willshoemaker, and (same as andrewhenry3), when I switched from ListView to CollectionView the problem went away.

I’m seeing the same issue as described by willshoemaker. If I switch from ListView to CollectionView the problem goes away.

I am currently on a severe deadline and barely have time to eat dinner, so, no. I cannot supply you with example code. I indicated as much from above. You guys are smart guys and should have no problem reproducing what I have reported.

I have this issue after I upgraded from .Net 7 to .Net 8 as well as the responsiveness of opening my page is much slower.

I have the same problem here. My bindings work as expected, but the xaml compiler throws me error messages. However, I can launch and use the application as expected. The problem occurs in the ListView for me.

Verified this issue with Visual Studio Enterprise 17.8.0 Preview 1.0. Can repro on windows platform. maui_bugs-main.zip MauiRepro8433-master.zip image

Similarly to the versions just above me; but for me CollectionView shows the same endless stream of wrong bindings and doesn’t end up loading anything and crashes out (though that might be something else’s fault). ListView shows the endless stream of wrong bindings but ends up falling back to the right thing and things are displayed properly, albeit seemingly with a performance issue.

Still happening to me even with CollectionView using the latest Maui packages for .NET 8 preview (8.0.0-preview.7.8842).

I’ve attached a minimal repro. Initial page load looks fine, but as soon as I hit the “Load more” button to modify the collection I see binding errors in the debugger. Seems the binding first fails by trying to bind to the view model and only then falls back to using the correct binding context (which in this case is the People property in the view model).

These binding failures spam my logs, and I am trying to understand if these also cause bad performance.

image

MauiApp1.zip