Skip to content

Try to infer DataContext type from the $parent and #named compiled binding path parts #17204

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

Merged
merged 7 commits into from
Oct 11, 2024

Conversation

maxkatz6
Copy link
Member

@maxkatz6 maxkatz6 commented Oct 6, 2024

What is the updated/expected behavior with this PR?

Enables shorter and cleaner syntax without explicit casts. Compiler can infer data context type from the same XAML file.

<Window x:Name="MyWindow" x:DataType="vm:TestDataContext">
    <TextBlock Text="{Binding #MyWindow.DataContext.StringProperty}" />
    <TextBlock Text="{Binding $parent[Window].DataContext.StringProperty}" />

What is the current behavior?

Developers need to cast DataContext to specific view model type, because DataContext is an object.

<Window x:Name="MyWindow" x:DataType="vm:TestDataContext">
    <TextBlock Text="{Binding #MyWindow.((vm:TestDataContext)DataContext).StringProperty}" />
    <TextBlock Text="{Binding $parent[Window].((vm:TestDataContext)DataContext).StringProperty}" />

Checklist

Fixed issues

Fixes #10957

@maxkatz6 maxkatz6 changed the title Try to infer DataContext type from the $parent and #named binding path parts Try to infer DataContext type from the $parent and #named compiled binding path parts Oct 6, 2024
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.3.999-cibuild0052425-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.3.999-cibuild0052429-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@timunie
Copy link
Contributor

timunie commented Oct 7, 2024

We should add a note to the docs once this is merged as older versions needs the cast and users may be confused otherwise.

@MrJul
Copy link
Member

MrJul commented Oct 7, 2024

A small unexpected change to $parent aside, this looks good!
Awesome work, can't wait to remove those ugly casts :)

@maxkatz6
Copy link
Member Author

@MrJul good find. Fixed.

@maxkatz6 maxkatz6 requested a review from MrJul October 11, 2024 00:00
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.3.999-cibuild0052545-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul added this pull request to the merge queue Oct 11, 2024
Merged via the queue into master with commit be7bb76 Oct 11, 2024
11 checks passed
@MrJul MrJul deleted the binding-datacontext-inheritance branch October 11, 2024 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Compiled Bindings] DataContext type should be inferred when using named elements in bindings
4 participants