Mot*_*oto 1 binding ivalueconverter xamarin.forms
我需要将标签绑定到一个复合值 - 由模型中的几个值组成。我正在尝试使用 ValueConverter 来执行此操作,但我无法弄清楚如何将对象本身传递给 ValueConverter。这是我的代码:在 XAML 中:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyApp;assembly=MyApp"
x:Class="MyApp.SavedDetailsPage">
<ContentPage.Resources>
<ResourceDictionary>
<local:DetailsConverter x:Key="detailsCvt" />
</ResourceDictionary>
</ContentPage.Resources>
...
<Label Text="{Binding ???, Converter={StaticResource detailsCvt}}" FontSize="Small" TextColor="Gray" />
...
Run Code Online (Sandbox Code Playgroud)
在 DetailsConverter.cs 中:
class DetailsConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
MyModel myModel = (MyModel)value;
return (myModel.FirstName + " " + myModel.LastName);
}
...
Run Code Online (Sandbox Code Playgroud)
我尝试使用“。” 用于绑定到 self ,但这不起作用。
我通过向 MyModel 添加一个 .This 属性找到了一种解决方法,它可以访问对象本身,因此我可以在 XAML 绑定中传递“This”,但不确定这是否是最好的方法。
提前致谢!
要绑定到 ViewModel,您可以使用以下语法之一
{Binding}
{Binding .}
{Binding Path="."}
Run Code Online (Sandbox Code Playgroud)