-
Notifications
You must be signed in to change notification settings - Fork 697
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
Proposal: Use BindBack function return value. #7888
Comments
does such as:
I need pass addional param |
Yeah, this is not how I expected I'm trying to convert from an With all the new source generator work being done in .NET, it'd be nice to see the XAML Compiler be part of the WinUI repo so that improvements for UWP and WindowsAppSDK XAML based generated code could be improved upon and shipped for these types of QoL improvements. They seem like easy wins. Agree that the output from the BindBack function should just be set to the property that's bound to (or if a function is called there, then whatever property is being used as the first parameter). That's going to be the common pattern in the majority of scenarios. This is how the doc example seems to imply that this should work: <TextBlock Text="{x:Bind a.MyFunc(b), BindBack=a.MyFunc2, Mode=TwoWay}" />
pushed back here seems to be the part that is missing that In another scenario of mine, I'm simply trying to bind an <muxc:NumberBox LargeChange="10"
Minimum="1"
SmallChange="2"
SpinButtonPlacementMode="Inline"
Value="{x:Bind help:Bind.ToDouble(MyIntValue), BindBack=help:Bind.ToInt, Mode=TwoWay}" /> And so I created two static helper functions to support the binding to and back as I would have in the past used an public static double ToDouble(int? value) => value ?? default;
public static int ToInt(double value) => (int)value; But this doesn't work. ☹ As // Generated binding code... does nothing...
private void UpdateTwoWay_9_Value()
{
if (this.initialized)
{
global::System.Double p0 = this.obj9.Value;
global::Helpers.Bind.ToInt(p0);
}
} Expect: private void UpdateTwoWay_9_Value()
{
if (this.initialized)
{
global::System.Double p0 = this.obj9.Value;
// TO ADD: Get 'obj' however the generator does for references...
obj.MyIntValue = global::Helpers.Bind.ToInt(p0);
}
} |
Yup, just gone back to I guess it at least still gets called more directly with the x:Bind codegen, but I think it still does a lookup to find the converter from the resources... Would be nice if we had a proper compiled x:Bind equivalent for this scenario. |
Does anybody know, is it possible to use ternary operator in function binding? |
@LeftTwixWand not sure why you thought to comment on this issue. Probably best to start a new discussion or ask on StackOverflow? But no, you can't use expressions in functional bindings. You'd have to pass the parameters in as arguments and then call it in the backing function. |
Proposal: Use BindBack function return value.
Summary
BindBack
does not do anything with the return value of the bound function. To match what anIValueConverter
does, the return value of the function should be assigned to the function parameter of the function bound inx:Bind
.This should work:
<TextBox Text="{x:Bind converters:Converters.IntegerToString(ViewModel.MyInteger), BindBack=converters:Converters.StringToInteger, Mode=TwoWay}"/>
But instead it is necessary to do:
<TextBox Text="{x:Bind converters:Converters.IntegerToString(ViewModel.MyInteger), BindBack=ViewModel.SetMyIntegerWithString, Mode=TwoWay}"/>
Rationale
IValueConverter
without having to createvoid Set[Property]With[Type]
methods in the ViewModel exclusively for the purpose ofBindBack
.void Set[Property]With[Type]
methods in the previous point break the MVVM separation since the methods are only forBindBack
, which is a feature the ViewModel should not be aware of.Important Notes
Since
IValueConverter
methods only have onevalue
parameter, this only works when there is only one function parameter bound inx:Bind
.The text was updated successfully, but these errors were encountered: