我需要将一个标签绑定到一个复合值--由模型中的几个值组成。我正在尝试使用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);
}
...我试着用“.”绑定到自我,但那不管用。
我找到了一种绕过它的方法:向.This添加一个MyModel属性,它允许访问对象本身,因此我可以在XAML绑定中传递'This‘,但不确定这是否是最好的方法。
提前感谢!
发布于 2017-05-18 09:15:53
要绑定到ViewModel,可以使用以下语法之一
{Binding}
{Binding .}
{Binding Path="."}https://stackoverflow.com/questions/44026060
复制相似问题