2017-08-24 3 views
0

Ist ListView die richtige Option, um Tabelle zu erstellen, so etwas?Xamarin.Forms multi Spalte Tabelle GUI

enter image description here

Zellinhalt ist nur Text, aber ich muß in der Lage sein, etwas zu zeigen, wie mit wenigen am häufigsten verwendeten Optionen-Menü auf Zelle Touch Drop-Down-und Textfeld für benutzerdefinierten Eintrag. Es wird bei maximal 80 bis 100 Zeilen Daten geben, normalerweise viel weniger.

+0

gibt es mehrere freie und kommerzielle DataGrid-Steuerelemente, die mit XF arbeiten – Jason

+0

Ich fand Syncfusion Data Grid-Komponente, aber ich denke, es ist übertrieben. Ich würde es vorziehen, keine Komponenten von Drittanbietern zu verwenden. – Primoz

Antwort

3

ListView ist in der Tat der beste Weg, um eine Tabelle anzunähern. Hier ist ein Beispiel ...

 <ListView x:Name="listViewm" ItemsSource="{Binding MyItems}"> 
      <ListView.Header> 
       <Grid BackgroundColor="Black"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="1*"></ColumnDefinition> 
         <ColumnDefinition Width="1*"></ColumnDefinition> 
         <ColumnDefinition Width="1*"></ColumnDefinition> 
         <ColumnDefinition Width="1*"></ColumnDefinition> 
        </Grid.ColumnDefinitions> 
        <Label Text="Switch" HorizontalOptions="Fill" Grid.Column="0" FontSize="Medium" FontAttributes="Bold" BackgroundColor="MediumBlue" TextColor="White" HorizontalTextAlignment="Center" Margin="1"/> 
        <Label Text="Addend 1" HorizontalOptions="Fill" Grid.Column="1" FontSize="Medium" FontAttributes="Bold" BackgroundColor="MediumBlue" TextColor="White" HorizontalTextAlignment="Center" Margin="1"/> 
        <Label Text="Addend 2" HorizontalOptions="Fill" Grid.Column="2" FontSize="Medium" FontAttributes="Bold" BackgroundColor="MediumBlue" TextColor="White" HorizontalTextAlignment="Center" Margin="1"/> 
        <Label Text="Result" HorizontalOptions="Fill" Grid.Column="3" FontSize="Medium" FontAttributes="Bold" BackgroundColor="MediumBlue" TextColor="White" HorizontalTextAlignment="Center" Margin="1"/> 
       </Grid> 
      </ListView.Header> 

      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell> 
         <Grid BackgroundColor="Black"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="1*"></ColumnDefinition> 
           <ColumnDefinition Width="1*"></ColumnDefinition> 
           <ColumnDefinition Width="1*"></ColumnDefinition> 
           <ColumnDefinition Width="1*"></ColumnDefinition> 
          </Grid.ColumnDefinitions> 
          <Label Grid.Column="0" Text ="{Binding Switch}" HorizontalOptions="Fill" BackgroundColor="LightBlue" HorizontalTextAlignment="Center" Margin="1"></Label> 
          <Label Grid.Column="1" Text ="{Binding Addend1}" HorizontalOptions="Fill" BackgroundColor="LightBlue" HorizontalTextAlignment="Center" Margin="1"></Label> 
          <Label Grid.Column="2" Text ="{Binding Addend2}" HorizontalOptions="Fill" BackgroundColor="LightBlue" HorizontalTextAlignment="Center" Margin="1"></Label> 
          <Label Grid.Column="3" Text ="{Binding Result}" HorizontalOptions="Fill" BackgroundColor="LightBlue" HorizontalTextAlignment="Center" Margin="1"></Label> 
         </Grid> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

Dies ist der Code in der View-Modell ...

public class MyItem : INotifyPropertyChanged 
{ 
    bool _switch = false; 
    public bool Switch 
    { 
     get 
     { 
      return _switch; 
     } 
     set 
     { 
      if (_switch != value) 
      { 
       _switch = value; 
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Switch")); 
      } 
     } 

    } 
    public int Addend1 { get; set; } 
    public int Addend2 { get; set; } 
    public int Result 
    { 
     get 
     { 
      return Addend1 + Addend2; 
     } 
    } 
    public string Summary 
    { 
     get 
     { 
      return Addend1 + " + " + Addend2 + " = " + Result; 
     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
} 

// ...

public MyItems ObservableCollection<MyItem> { get; set; } 

// ...

 MyItems = new ObservableCollection<MyItem>(); 
     MyItems.Add(new MyItem() { Switch = true, Addend1 = 1, Addend2 = 2 }); 
     MyItems.Add(new MyItem() { Switch = false, Addend1 = 1, Addend2 = 2 }); 
     MyItems.Add(new MyItem() { Switch = true, Addend1 = 2, Addend2 = 3 }); 
     MyItems.Add(new MyItem() { Switch = false, Addend1 = 2, Addend2 = 3 }); 

Dies ergibt eine Tabelle, die so aussieht ...

ListView as table

nicht sicher, warum Rinnen zwischen den Säulen sind so breit.

Sie können Menüelemente auf ViewCell.ContextActions definieren.