WinUI 中的 x:Bind 函数

WinUI 应用允许您在 {x:Bind} 标记扩展中将函数用作数据绑定路径中的叶节点步骤。 此功能简化了值转换,并使绑定能够依赖于多个参数,使应用更具动态性和效率。

小窍门

有关在应用中对 {x:Bind} 使用数据绑定的一般信息(有关 {x:Bind}{Binding}之间的全面比较),请参阅 数据绑定深入{x:Bind} 标记扩展

在以下示例中,项的背景和前景绑定到基于颜色参数进行转换的函数。

<DataTemplate x:DataType="local:ColorEntry">
    <Grid Background="{x:Bind local:ColorEntry.Brushify(Color), Mode=OneWay}" Width="240">
        <TextBlock Text="{x:Bind ColorName}" Foreground="{x:Bind TextColor(Color)}" Margin="10,5" />
    </Grid>
</DataTemplate>
public class ColorEntry
{
    public string ColorName { get; set; }
    public Color Color { get; set; }

    public static SolidColorBrush Brushify(Color c)
    {
        return new SolidColorBrush(c);
    }

    public SolidColorBrush TextColor(Color c)
    {
        return new SolidColorBrush(((c.R * 0.299 + c.G * 0.587 + c.B * 0.114) > 150) ? Colors.Black : Colors.White);
    }
}

XAML 属性用法

<object property="{x:Bind pathToFunction.FunctionName(functionParameter1, functionParameter2, ...), bindingProperties}" ... />

函数的路径

像其他属性路径一样指定 函数的路径 。 路径可以包含 (.)、索引器强制转换 来定位函数。

使用 XMLNamespace:ClassName.MethodName 语法指定静态函数。 例如,使用以下语法绑定到后台代码中的静态方法。

<Window 
     xmlns:local="using:MyNamespace">
     ...
    <StackPanel>
        <TextBlock x:Name="BigTextBlock" FontSize="20" Text="Big text" />
        <TextBlock FontSize="{x:Bind local:MyHelpers.Half(BigTextBlock.FontSize)}" 
                   Text="Small text" />
    </StackPanel>
</Window>
namespace MyNamespace
{
    static public class MyHelpers
    {
        public static double Half(double value) => value / 2.0;
    }
}

还可以直接在标记中使用系统函数来完成诸如日期格式、文本格式、文本串联以及其他简单情况。 例如:

<Window 
     xmlns:sys="using:System"
     xmlns:local="using:MyNamespace">
     ...
     <CalendarDatePicker Date="{x:Bind sys:DateTime.Parse(TextBlock1.Text)}" />
     <TextBlock Text="{x:Bind sys:String.Format('{0} is now available in {1}', local:MyPage.personName, local:MyPage.location)}" />
</Window>

如果将模式设置为 OneWay 或 TwoWay,则函数路径支持更改检测。 如果这些对象发生更改,绑定引擎将重新评估绑定。

您要绑定的函数需要:

  • 能够访问代码和元数据——因此在 C# 中,“内部或私有”访问限定符可以工作,但在 C++ 中,方法需要是公共的 WinRT 方法。
  • 支持基于参数数(而不是类型)进行重载,并尝试将第一个重载与该多个参数匹配
  • 具有与传入的数据匹配的参数类型 - 绑定引擎不执行缩小转换
  • 具有与进行绑定的属性类型匹配的返回类型

绑定引擎会响应使用函数名称触发的属性更改通知,并根据需要重新评估绑定。 例如:

<DataTemplate x:DataType="local:Person">
   <StackPanel>
      <TextBlock Text="{x:Bind FullName}" />
      <Image Source="{x:Bind IconToBitmap(Icon, CancellationToken), Mode=OneWay}" />
   </StackPanel>
</DataTemplate>
public class Person : INotifyPropertyChanged
{
    //Implementation for an Icon property and a CancellationToken property with PropertyChanged notifications
    ...

    //IconToBitmap function is essentially a multi binding converter between several options.
    public Uri IconToBitmap (Uri icon, Uri cancellationToken)
    {
        var foo = new Uri(...);        
        if (isCancelled)
        {
            foo = cancellationToken;
        }
        else 
        {
            if (fullName.Contains("Sr"))
            {
               //pass a different Uri back
               foo = new Uri(...);
            }
            else
            {
                foo = icon;
            }
        }
        return foo;
    }

    //Ensure FullName property handles change notification on itself as well as IconToBitmap since the function uses it
    public string FullName
    {
        get { return fullName; }
        set
        {
            fullName = value;
            OnPropertyChanged();
            OnPropertyChanged("IconToBitmap"); 
            //this ensures Image.Source binding re-evaluates when FullName changes in addition to Icon and CancellationToken
        }
    }
}

小窍门

使用函数 x:Bind 来实现与 WPF 中通过转换器和 MultiBinding 支持的方案相同的场景。

函数参数

指定用逗号分隔的多个函数参数(,)。

  • 绑定路径 – 使用与直接绑定到对象时相同的语法。
    • 如果将模式设置为 OneWay 或 TwoWay,绑定将检测更改,并在对象更改时重新计算。
  • 用引号括起来的常量字符串 – 包含引号以将其指定为字符串。 使用 hat (^) 在字符串中转义引号。
  • 常数 – 例如 -123.456。
  • 布尔值 – 指定为“x:True”或“x:False”。

小窍门

TargetNullValue 适用于函数调用的结果,不适用于任何绑定参数。

双向函数绑定

在双向绑定方案中,必须为绑定的反向方向指定第二个函数。 使用此函数的 BindBack 绑定属性。 在下面的示例中,该函数采用一个参数,即需要推送回模型的值。

<TextBlock Text="{x:Bind a.MyFunc(b), BindBack=a.MyFunc2, Mode=TwoWay}" />

另请参阅