是否可以有2个绑定来源?来自CellView等元素?
我搜索并尝试了我脑海中的几种可能性而无法做我想做的事情。看起来很简单..
我说我有这个页面(我放了一个简单的代码,它只是有点长,但并不难):
MainPage.cs
public partial class CoursesHistory : ContentPage
{
// Course it's the element "data template" for the listView
private class Course
{
public string Date { get; set; }
public string Destination { get; set; }
private string _price;
public string Price {
get { return this._price; }
set
{
this._price = "€" + value;
}
}
}
// FontAttribute it's the other element which is not set as ItemsSource, but used from the cell to get the FontFamily ask, the FontSize etc
private class FontAttributes
{
public string MOON_FAMILY_ALIEN_BOLD { get; set; }
....
public FontAttributes()
{
....
}
}
public FontAttributes FontAttr;
public CoursesHistory()
{
FontAttr = new FontAttributes();
InitializeComponent();
InitList();
}
private void InitList()
{
ObservableCollection<Course> courses = new ObservableCollection<Course>();
ListViewHistory.ItemsSource = courses;
courses.Add(new Course() { Date = "1 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "2 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "3 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "4 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "5 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "6 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "7 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "8 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "9 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "10 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "11 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "12 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "13 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "14 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
courses.Add(new Course() { Date = "15 Novembre 1995", Destination = "Le Mans", Price = "23.11" });
}
}
然后是其XAML MainPage.xaml.cs
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Pages.CoursesHistory">
<AbsoluteLayout>
<ListView x:Name="ListViewHistory" BackgroundColor="White" RowHeight="50"
AbsoluteLayout.LayoutBounds="0.5,0.5,1,1"
AbsoluteLayout.LayoutFlags="All">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<AbsoluteLayout>
列表与我的ObservableCollection绑定,那么如何访问未绑定的FontAttr?
<Label FontFamily="{Binding Path=HOW_CAN_I_ACCESS_FONTATTR??}"
Text="{Binding Date}" TextColor="Black" FontSize="14" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"
AbsoluteLayout.LayoutBounds="0, 0.5, 0.3, 1"
AbsoluteLayout.LayoutFlags="All"/>
<Label Text="{Binding Destination}" TextColor="Black" FontSize="14" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"
AbsoluteLayout.LayoutBounds="0.5, 0.5, 0.5, 1"
AbsoluteLayout.LayoutFlags="All"/>
<Label Text="{Binding Price}" TextColor="Black" FontSize="14" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"
AbsoluteLayout.LayoutBounds="1, 0.5, 0.2, 1"
AbsoluteLayout.LayoutFlags="All"/>
</AbsoluteLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</AbsoluteLayout>
</ContentPage>
感谢阅读和帮助!
答案 0 :(得分:2)
在XAML中,您应指定字符串格式以显示双精度值。
<StackLayout Orientation="Horizontal">
<Label x:Name="Name_Label_MeetingLocation1"
Text="{Binding Latitude ,
StringFormat='{0:0.0000}'}"/>
</StackLayout>
答案 1 :(得分:0)
您需要使用相对绑定来实现此目的。
首先,您需要将FontAttr转换为属性
public FontAttributes FontAttr { get; set; }
然后你需要将BindingContext设置为页面本身,如下所示
public CoursesHistory()
{
FontAttr = new FontAttributes();
InitializeComponent();
InitList();
BindingContext=this;
}
现在您可以像这样使用相对绑定:
<ListView x:Name="ListViewHistory">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<AbsoluteLayout>
<Label FontFamily="{Binding Path=BindingContext.FontAttr.MOON_FAMILY_ALIEN_BOLD, Source={x:Reference Name=ListViewHistory}}"
Text="{Binding Date}" TextColor="Black" FontSize="14" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"
AbsoluteLayout.LayoutBounds="0, 0.5, 0.3, 1"
AbsoluteLayout.LayoutFlags="All"/>
<Label Text="{Binding Destination}" TextColor="Black" FontSize="14" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"
AbsoluteLayout.LayoutBounds="0.5, 0.5, 0.5, 1"
AbsoluteLayout.LayoutFlags="All"/>
<Label Text="{Binding Price}" TextColor="Black" FontSize="14" VerticalTextAlignment="Center" HorizontalTextAlignment="Center"
AbsoluteLayout.LayoutBounds="1, 0.5, 0.2, 1"
AbsoluteLayout.LayoutFlags="All"/>
</AbsoluteLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
注意我使用Source = {x:Reference Name = ListViewHistory}进行相对绑定。