-
Notifications
You must be signed in to change notification settings - Fork 710
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
Binding is hard to do in C++ #6411
Comments
@RealTommyKlein FYI |
We've actually been having some internal discussions around this and larger improvements to the C++ experience - will put the blurb about Binding vs. x:Bind, but the current thinking is there are scenarios only possible with X:Bind vs. Binding OverviewToday in Xaml, we have two different built-in ways of achieving data binding:
{Binding} pros
{Binding} consHowever, as developers used
BindingControl.xaml:<UserControl x:Class="Foo.BindingControl">
<TextBlock Text="{Binding Text, ElementName=SomeTextControl}" />
</UserControl> In BindingControl.xaml, there is no element named "SomeTextControl", so we might think this Binding should be invalid and raise an error at compile time. However, because Binding evaluates the visual tree at runtime, using it like this would work since now there is a context where "SomeTextControl" is a valid element: <Page x:Class="Bar.Page">
<TextBox Text="some text" x:Name="SomeTextControl" />
<foo:BindingControl />
</Page> But in general, the Xaml compiler can't analyze this case - imagine BindingControl was defined in a control library, and the sample markup above was from an app consuming it. When the Xaml compiler is building the control library, it has no way of knowing how the control will be used, and when compiling the application, the Xaml compiler doesn't know the source .xaml content for BindingControl to validate its final usage.
{x:Bind} pros{x:Bind} was designed to address these issues with {Binding}. Instead of an actual {Binding} object created at runtime, when the Xaml compiler sees an {x:Bind} in markup, it generates custom code to implement the binding logic, which is compiled into the component/library being built. {x:Bind} has great compile time validation. The DataContext and types used by x:Bind are all known at the Xaml compiler's compilation time. The Xaml compiler can verify all types/properties used by x:Bind, and raise a descriptive compile-time error instead of a difficult to diagnose runtime error. Because x:Bind is implemented in compiled code, the Xaml compiler doesn't have to worry about generating type information for the types, and developer never need to add the {x:Bind} has better performance than {Binding}. The binding logic is implemented as compiled code, not runtime reflection, enabling more optimization. {x:Bind} has newer features. Because development shifted to x:Bind over Bindings, x:Bind has received new features that Binding doesn't have, like the ability to use functions in x:Binds (called "function binding"). {x:Bind} consSince developers have used x:Bind, it has several apparent flaws:
|
I'd add an {x:Bind} con. You can't easily do this using {x:bind} (or at all?): <UserControl Name="ThisControl" ... />
<DataTemplate x:DataType="a:b">
...
<ColumnDefinition Width="{Binding Path=SomeValue, ElementName=ThisControl, Mode=OneWay}" /> |
@kmgallahan yes, that's a big problem with x;Bind is scoping resolution doesn't seem to work as intended. There's an open issue on this: #2508 If some time to improve x:Bind could be focused on, it'd solve a lot of pain points with application development in general. Would be nice to make it easier to use for templated control development auto-generating glue for |
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
x:Bind
is a lot easier but the two mechanisms fulfill different scenarios (runtime vs. build time). For C++, one has to implementICustomPropertyProvider
: https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/binding-property#using-the-binding-markup-extension-with-cwinrtThe text was updated successfully, but these errors were encountered: