Reputation: 3964
Is it possible to have 2 sources of binding? from an element such as CellView?
I searched and tried several possibilities I had in my mind without be able to do what I want to do. It seems so simple however..
Let says I have this page (I put a simple code, it is just a bit long, but not hard):
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" });
}
}
and then its 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>
The list is Bind with my ObservableCollection so how can I access FontAttr which is not bind?
<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>
Thank for read and for your help !
Upvotes: 0
Views: 1867
Reputation: 216
You need to use Relative Binding to achieve this.
First, you need to convert FontAttr to properties
public FontAttributes FontAttr { get; set; }
Then you need to set BindingContext to page itself as follows
public CoursesHistory()
{
FontAttr = new FontAttributes();
InitializeComponent();
InitList();
BindingContext=this;
}
Now you can use Relative Binding like 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>
Note I use Source={x:Reference Name=ListViewHistory} for relative binding.
Upvotes: 0
Reputation: 21
In XAML, you should specify string format to display an double value.
<StackLayout Orientation="Horizontal">
<Label x:Name="Name_Label_MeetingLocation1"
Text="{Binding Latitude ,
StringFormat='{0:0.0000}'}"/>
</StackLayout>
Upvotes: 2