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

[XC] Infer x:DataType from BindingContext or BindingExtension.Source #21834

Open
9 tasks
simonrozsival opened this issue Apr 15, 2024 · 4 comments
Open
9 tasks
Labels
area-xaml XAML, CSS, Triggers, Behaviors proposal/open

Comments

@simonrozsival
Copy link
Member

simonrozsival commented Apr 15, 2024

In certain scenarios, XamlC should be able to infer the x:DataType based on the value passed to BindingContext on any XAML element or to Source on BindingExtension elements:

  • {StaticResource X} - in the case when X is defined in the same XAML document
  • {x:Static Y}
  • {x:Reference Z}
  • {Binding W} - when the binding is compiled
  • {Binding Source={RelativeSource U}}:
    • Self - when the elemet to which the binding is set is statically known
    • FindAncestor
    • FindAncestorBindingContext - when the ancestor and the type of the binding context can be statically resolved
    • TemplatedParent - when the templated parent can be statically resolved

Explicitly declared x:DataType should always take precedence over the inferred type. In the case when there is a mismatch, we should consider emitting a warning.

Are there any other cases which we could infer?

/cc @StephaneDelcroix

@hansmbakker
Copy link

Will this still be part of .NET 9? I'm afraid a lot of people will run into this via #23711 if they are upgrading from .NET 9

@simonrozsival
Copy link
Member Author

@hansmbakker it's not likely this will be in .NET 9. It's true that the issue you linked shows that people might find it difficult to upgrade to .NET 9 in certain scenarios and we need to do something about it (most likely better warning messages).

I don't think implementing the type inference the way I outlined it in this issue would fix that issue though. The problem there is that the Path can only be resolved at runtime via reflection and it cannot be compiled ahead of time because we don't know the type of object BindingContext and we don't have a way to specify a cast at the moment.

@awasilik
Copy link

Without this we'll not be able to compile app with .net9 as we need to use binding to Source{x:Reference

@MartyIX
Copy link
Contributor

MartyIX commented Sep 16, 2024

@awasilik This should work:

-<RowDefinition Height="{Binding HeightRequest, Source={x:Reference thisControl}}"/>
+<RowDefinition Height="{Binding HeightRequest, Source={x:Reference thisControl}, x:DataType={x:Null}}"/>

However, yes, it requires manual changes in code.

david-maw added a commit to david-maw/ItemSelfRef that referenced this issue Oct 16, 2024
Because dotnet/maui#21834 isn't implemeted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-xaml XAML, CSS, Triggers, Behaviors proposal/open
Projects
None yet
Development

No branches or pull requests

7 participants