備註
如需在應用程式中使用 {x:Bind} 進行資料繫結的一般資訊(以及 {x:Bind} 與 {Binding}之間的完整比較),請參閱 資料繫結深入探討 及 {x:Bind} 標記延伸。
從 Windows 10 版本 1607 開始,{x:Bind} 支援使用函式作為系結路徑的分葉步驟。 這會啟用以下功能:
- 達成值轉換的更簡單方式
- 系結相依於多個參數的方式
備註
若要搭配 {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>
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 語法來指定靜態函式。 例如,使用下列語法來繫結到程式碼隱藏中的靜態函數。
<Page
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>
</Page>
namespace MyNamespace
{
static public class MyHelpers
{
public static double Half(double value) => value / 2.0;
}
}
您也可以直接在標記中使用系統函式來完成日期格式設定、文字格式設定、文字串連等簡單案例,例如:
<Page
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)}" />
</Page>
如果模式為 OneWay/TwoWay,則會對函式路徑進行變更偵測,如果這些物件有變更,則會重新評估系結。
被綁定的函式需要:
- 使程式碼和元數據可存取 – 因此,內部或私人工作可以使用 C#,但是 C++/CX 中的方法需要是公用的 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)
{
Uri foo = new Uri(...);
if (isCancelled)
{
foo = cancellationToken;
}
else
{
if (this.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 this.fullName; }
set
{
this.fullName = value;
this.OnPropertyChanged ();
this.OnPropertyChanged ("IconToBitmap");
//this ensures Image.Source binding re-evaluates when FullName changes in addition to Icon and CancellationToken
}
}
}
小提示
您可以使用 x:Bind 中的函式來達成與 WPF 中的 Converters 和 MultiBinding 支援的相同情境。
函式參數
您可以指定多個函式自變數,並以逗號的 (,) 分隔
- 系結路徑 – 與直接系結至該對象的語法相同。
- 如果模式為 OneWay/TwoWay,則會執行變更偵測,並在物件變更時重新評估系結
- 以引弧括住的常數位符串 – 必須加上引號,才能將它指定為字串。 Hat (^)可用來跳脫字串中的引號
- 常數 - 例如 -123.456
- 布爾值 – 指定為 “x:True” 或 “x:False”
小提示
TargetNullValue 將會套用至函數調用的結果,而不是套用至任何系結自變數。
雙向函數綁定
在雙向系結案例中,必須針對系結的反向方向指定第二個函式。 這是使用 BindBack 系結屬性來完成。 在下列範例中,函式應該採用一個自變數,這是需要推送回模型的值。
<TextBlock Text="{x:Bind a.MyFunc(b), BindBack=a.MyFunc2, Mode=TwoWay}" />