Forum Community

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

http://forums.xamarin.com/discussion/42792/two-columns-per-row-in-listview

optimisererpoptimisererp INMember

I need help in the code in the forum above stated

Tagged:

Answers

  • optimisererpoptimisererp INMember

    According to above code i have made the following.
    1. I am not able how to add data to it.
    2. Is any calling of view model is req.

    My Code ..
    1. My Xaml (MyPage.xaml)

    <?xml version="1.0" encoding="utf-8" ?>

    <ContentPage.BindingContext>

    </ContentPage.BindingContext>

    <!-- BindingContext here is ModelPair, which has Item1 and Item2 -->
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
    
          <!-- Two-column grid -->
          <Grid>
    
            <Grid.RowDefinitions>
              <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="50*" />
              <ColumnDefinition Width="50*" />
            </Grid.ColumnDefinitions>
    
            <!-- Left column - BindingContext set to ModelPair.Item1  -->
            <v:ListViewColumnView Grid.Column="0"
                                  BindingContext="{Binding Item1}"/>
    
            <!-- Right column - BindingContext set to ModelPair.Item2 -->
            <v:ListViewColumnView Grid.Column="1"
                                  BindingContext="{Binding Item2}">
    
              <!-- Don't show right column for "empty/invalid" models -->
              <v:ListViewColumnView.Triggers>
                <DataTrigger TargetType="v:ListViewColumnView"
                             Binding="{Binding Id}"
                             Value="-1">
                  <Setter Property="IsVisible" Value="False"/>
                </DataTrigger>
              </v:ListViewColumnView.Triggers>
    
            </v:ListViewColumnView>
    
          </Grid>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
    


    CS File
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    using Xamarin.Forms;

    namespace ChicagoDelight_Portable_
    {
    public partial class MyPag : ContentPage
    {
    public MyPag()
    {
    InitializeComponent();

        }
    
    }
    

    }

    /////// view model --- In Folder ViewModel Page MyViewModel

    using System;

    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    namespace ChicagoDelight_Portable_.ViewModels
    {

    public class MyViewModel
    {
    
        /// <summary>
        /// All models; loaded from appropriate data source.
        /// </summary>
        /// 
    
        public ObservableCollection<MyModel> AllModels { get; set; }
    
    
        /// <summary>
        /// Model pairs, used for two-column list items.
        /// </summary>
        public ObservableCollection<ModelPair> ModelPairs { get; set; }
    
        public MyViewModel()
        {
    
            AllModels = new ObservableCollection<MyModel>();
            ModelPairs = new ObservableCollection<ModelPair>();
            CreateModelPairs();
        }
    
        /// <summary>
        /// Creating model pairs from all available model instances.
        /// </summary>
        private void CreateModelPairs()
        {
            for (int i = 0; i < AllModels.Count; i += 2)
            {
                MyModel item1 = AllModels[i];
                MyModel item2 = i + 1 < AllModels.Count ? AllModels[i + 1] : null;
    
                ModelPairs.Add(new ModelPair(item1, item2));
            }
        }
    }
    
    ///
    /// It's not necessary to use Tuple here
    /// Item1 used in left column; Item2 used in right column
    ///
    public class ModelPair : Tuple<MyModel, MyModel>
    {
        public ModelPair(MyModel item1, MyModel item2)
            : base(item1, item2 ?? CreateEmptyModel()) { }
    
        private static MyModel CreateEmptyModel()
        {
            return new MyModel
            {
                Id = -1 // -1 indicates that view should not be rendered
            };
        }
    }
    
    public class MyModel
    {
        public int Id { get; set; }
    }
    

    }

    //// Calling

    this.Add(new MenuItem()
    {
    Titulo = "Delivery Address",
    TargetType = typeof(MyPag) // calling
    });

    Result ...

    Blank Page

Sign In or Register to comment.