Skip to content

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

@ghost

Description

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

Metadata

Metadata

Labels

area-controls-listviewListView and TableViewplatform/windowss/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions