Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TabControl.ContentTemplate uses wrong DataContext #2 #445

Closed
SimonORorke opened this issue Apr 5, 2024 · 7 comments
Closed

TabControl.ContentTemplate uses wrong DataContext #2 #445

SimonORorke opened this issue Apr 5, 2024 · 7 comments

Comments

@SimonORorke
Copy link
Contributor

SimonORorke commented Apr 5, 2024

This is a follow on from closed Issue 13909 TabControl.ContentTemplate uses wrong DataContext, as I suggest it warrants further discussion. Let me reformulate the problem in a more long-winded way.

Following the instructions in the How To Bind Tabs page of Avalonia's documentation, in MainWindow.axaml I bound a TabControl to a collection property of MainWindowViewModel, like this:

<TabControl x:Name="TabControl"
			ItemsSource="{Binding Tabs}"
			SelectedItem="{Binding SelectedTab}">
	<TabControl.ItemTemplate>
		<DataTemplate>
			<TextBlock Text="{Binding Header}" />
		</DataTemplate>
	</TabControl.ItemTemplate>
	<TabControl.ContentTemplate>
		<DataTemplate>
			<ContentControl Content="{Binding ViewModel}" />
		</DataTemplate>
	</TabControl.ContentTemplate>
</TabControl>

The problem is apparent when looking at the same XAML in Rider:
TabControl Binding Error
TabControl.ItemTemplate, which defines the tab header, correctly binds to a property in the repeating item of the collection to which ItemsSource is bound. However, TabControl.ContentTemplate, which defines the content shown below the tab, incorrectly attempts to bind to a a non-existent property of the Window's view model instead of the required property of the repeating item.

The solution, as I found in Issue 13909, is to add a DataType to TabControl.ContentTemplate's DataTemplate, like this:

<TabControl.ContentTemplate>
	<DataTemplate DataType="viewModel:TabItemViewModel">
		<ContentControl Content="{Binding ViewModel}" />
	</DataTemplate>
</TabControl.ContentTemplate>

Is the difference between the binding behaviours of TabControl.ItemTemplate and TabControl.ContentTemplate a bug? Or is it as expected?

If it's not a bug, or perhaps till any bug has been fixed, the How To Bind Tabs documentation needs to be corrected.

@SimonORorke SimonORorke changed the title NVM, got it working by specifying DataType in TabControl.ContentTemplate->DataTemplate TabControl.ContentTemplate uses wrong DataContext #2 Apr 5, 2024
@timunie
Copy link
Contributor

timunie commented Apr 5, 2024

It's by design when using compiled bindings as ItemTemplate can guess datatype if collections item type is known. ContentTemplate can't do that afaik as it could be any contentent if developers want to. So its a lack in the docs as these were written before we had CompiledBindings. A PR to fix docs is appreciated.

@timunie timunie transferred this issue from AvaloniaUI/Avalonia Apr 5, 2024
@SimonORorke
Copy link
Contributor Author

Thanks for the explanation. What is a PR? Is it something you would like me to do to request the documentation fix?

@timunie
Copy link
Contributor

timunie commented Apr 5, 2024

PR = "Pull Request". Our docs are written in MarkDown and everyone can file a fix.

image

@SimonORorke
Copy link
Contributor Author

OK, I'm happy to do it, but don't want to mess up, as it's my first time. Let's see if I've understood the procedure. I can see I could edit in place. But it is not obvious to me that that's what I should do. Do I need to create a branch with the edit for someone to approve?

@timunie
Copy link
Contributor

timunie commented Apr 5, 2024

No worries, you don't have write access anyways. So it should create a branch for you and a PR

@SimonORorke
Copy link
Contributor Author

Got it. I’ll give it a go in a few hours, it’s 2:15am here in New Zealand.

@SimonORorke
Copy link
Contributor Author

I have submitted the pull request.

SimonORorke added a commit to SimonORorke/avalonia-docs that referenced this issue Apr 5, 2024
SimonORorke added a commit to SimonORorke/avalonia-docs that referenced this issue Apr 5, 2024
SimonORorke added a commit to SimonORorke/avalonia-docs that referenced this issue Apr 6, 2024
SimonORorke added a commit to SimonORorke/avalonia-docs that referenced this issue Apr 8, 2024
@timunie timunie closed this as completed Apr 8, 2024
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants