2016-06-28 11 views
0

Ist es möglich, 2 Bindungsquellen zu haben? von einem Element wie CellView?Xamarin C# Double Binding Source

Ich suchte und probierte mehrere Möglichkeiten, die ich in meinem Kopf hatte, ohne in der Lage sein zu können, was ich tun wollte. Es scheint so aber einfach ..

Let sagt, ich habe diese Seite (ich einen einfachen Code zu setzen, ist es nur ein bisschen lang, aber nicht hart):

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" }); 
    } 
} 

und seine XAML dann 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> 

Die Liste ist Bind mit meiner ObservableCollection, also wie kann ich FontAttr zugreifen, die nicht binden?

   <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> 

Dank für Lesen und für Ihre Hilfe!

+0

Erstellen Sie ein benutzerdefiniertes Etikett mit IMarkup erweitert und benutzerdefinierte Bindungen erstellen, siehe https://codemilltech.com/back-to-school-adding-a-custom-bindable-property-to-xamarin-forms/ –

+0

Machen Sie Ihre 'FontAttr 'Feld eine öffentliche Eigenschaft. Ich bin nicht vertraut mit Xamarin, aber es scheint, dass Sie einen 'BindingContext' setzen können. Setze 'BindingContext' auf * self * und mache den üblichen BindingStuff – lokusking

+0

@AthulHarikumar Ok Ich schaue mal – Emixam23

Antwort

2

In XAML sollten Sie das Zeichenfolgenformat angeben, um einen doppelten Wert anzuzeigen.

<StackLayout Orientation="Horizontal"> 

    <Label x:Name="Name_Label_MeetingLocation1" 
     Text="{Binding Latitude , 
     StringFormat='{0:0.0000}'}"/> 

</StackLayout> 
+0

Es beantwortet die Frage nicht. –

0

Sie müssen Relative Bindung verwenden, um dies zu erreichen.

Zuerst müssen Sie FontAttr zu Immobilien

public FontAttributes FontAttr { get; set; } 

Dann Sie Binding auf Seite selbst festlegen müssen konvertieren wie

folgt
public CoursesHistory() 
{ 
    FontAttr = new FontAttributes(); 
    InitializeComponent(); 
    InitList(); 
    BindingContext=this; 
} 

Jetzt können Sie Relative wie diese Bindung verwenden:

<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> 

Hinweis: Ich verwende Quelle = {x: Referenzname = ListViewHistory} für relative bin ding.