ValueConverter - 绑定到自己
ValueConverter - binding to self
我需要将标签绑定到复合值 - 由模型中的几个值组成。我正在尝试使用 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" />
...
在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);
}
...
我试过使用“.”用于绑定到自己,但这没有用。
我通过向 MyModel 添加一个 .This 属性 找到了解决它的方法,它可以访问对象本身,因此我可以在 XAML 绑定中传递 'This',但不确定这是否是最好的方法。
提前致谢!
要绑定到 ViewModel,您可以使用以下语法之一
{Binding}
{Binding .}
{Binding Path="."}
我需要将标签绑定到复合值 - 由模型中的几个值组成。我正在尝试使用 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" />
...
在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);
}
...
我试过使用“.”用于绑定到自己,但这没有用。
我通过向 MyModel 添加一个 .This 属性 找到了解决它的方法,它可以访问对象本身,因此我可以在 XAML 绑定中传递 'This',但不确定这是否是最好的方法。
提前致谢!
要绑定到 ViewModel,您可以使用以下语法之一
{Binding}
{Binding .}
{Binding Path="."}