Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 如何将两个属性绑定到TextCell的Detail属性_Xamarin - Fatal编程技术网

Xamarin 如何将两个属性绑定到TextCell的Detail属性

Xamarin 如何将两个属性绑定到TextCell的Detail属性,xamarin,Xamarin,在我的代码隐藏中,我创建了一个具有以下属性的RecentSearchs类: public class RecentSearches { public string Location { get; set; } public string CheckIn { get; set; } public string CheckOut { get; set; } } 我创建了该类型的通用列表,并将其实例化为以下值lvRecentSearches是XAML

在我的代码隐藏中,我创建了一个具有以下属性的RecentSearchs类:

public class RecentSearches
{
        public string Location { get; set; }
        public string CheckIn { get; set; }
        public string CheckOut { get; set; }

}
我创建了该类型的通用列表,并将其实例化为以下值lvRecentSearches是XAML代码中的ListView对象

lvRecentSearches.ItemsSource = new List<RecentSearches>
{
    new RecentSearches
    {
        Location = "West Hollywood, CA, United States",
        CheckIn = new DateTime(2016, 9, 1).ToString("MMM dd, yyyy"),
        CheckOut = new DateTime(2016, 11, 1).ToString("MMM dd, yyyy"),
    },
    new RecentSearches
    {
        Location = "Santa Monica, CA, United States",
        CheckIn = new DateTime(2016, 9, 1).ToString("MMM dd, yyyy"),
        CheckOut = new DateTime(2016, 11, 1).ToString("MMM dd, yyyy"),
    }
};
lvRecentSearches.ItemsSource=新列表
{
最新搜索
{
地点=“美国加利福尼亚州西好莱坞”,
签入=新日期时间(2016年9月1日)。ToString(“MMM dd,yyyy”),
签出=新日期时间(2016年11月1日)。ToString(“MMM dd,yyyy”),
},
最新搜索
{
地点=“美国加利福尼亚州圣莫尼卡”,
签入=新日期时间(2016年9月1日)。ToString(“MMM dd,yyyy”),
签出=新日期时间(2016年11月1日)。ToString(“MMM dd,yyyy”),
}
};
在XAML中,我想将TextCell的Detail属性设置为CheckIn和CheckOut属性。这是可能的,还是应该在代码隐藏中合并它们

<ListView x:Name="lvRecentSearches">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Location}" Detail="{Binding CheckIn}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

一个简单的方法就是创建第四个属性

公共字符串CheckInAndOut{get{return CheckIn+“”+CheckOut;}}


然后将此属性绑定到TextCell的详细信息。

可以创建自定义的
ViewCell
,然后在
数据模板中使用它。解释得很好。这允许您自定义单元格模板,从而将任何类型的数据绑定到其中。您可以轻松地为签入/签出属性添加两个相邻的标签


请注意,由于渲染过程,通常
TextCell
的性能更好。如果您的目标只是将两个属性重新组合在一个属性中,同时保持
TextCell
的外观,我建议您在代码隐藏中执行此操作,然后将此新属性绑定到
Detail
属性。因为在您的情况下,自定义
ViewCell
可能需要大量工作(即自定义渲染器)才能获得大的附加值。

为什么不使用将两个转换为一个的转换器呢?