Привязка данных ListView с пользовательским классом - PullRequest
0 голосов
/ 20 января 2019

Я хотел бы привязать данные к пользовательскому классу при отображении в xmal, но в представлении списка ничего не отображается.Любые указатели на то, что мне не хватает, будут высоко оценены.

  public Transactional()
    {

        string oCustomerLoggedIn = Preferences.Get("uLoginUsername", "default_value");

        int oCustomerLoggedInId = GetCustomerId(oCustomerLoggedIn);



        List<TransactionsList> custTransactions  = ViewCustomerTransactions(oCustomerLoggedInId);




        InitializeComponent();



        listView.SetBinding(ListView.ItemsSourceProperty, new Binding("."));
        listView.BindingContext = custTransactions;
    }





  <ListView x:Name="listView" ItemSelected="OnItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.ContextActions>
                        <MenuItem Clicked="OnMore" CommandParameter="{Binding .}" Text="More" />
                        <MenuItem Clicked="OnDelete" CommandParameter="{Binding .}" Text="Delete" IsDestructive="True" />
                    </ViewCell.ContextActions>
                    <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                        <StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
                            <Label Text="{Binding .}" VerticalTextAlignment="Center" FontSize="Medium" />
                           </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

1 Ответ

0 голосов
/ 20 января 2019

Попробуйте, выполните рефакторинг и измените, где это необходимо ...

Модель элемента списка ...

using System;

namespace Playpen
{
    public class ListItem
    {
        public string Text { get; set; }
        public string SubText { get; set; }
    }
}

XAML ...

<?xml version="1.0" encoding="utf-8"?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:Playpen" 
    x:Class="Playpen.MainPage">

    <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">               
        <ListView 
            ItemsSource="{Binding DataSource}"
            HasUnevenRows="true"
            HorizontalOptions="FillAndExpand"
            VerticalOptions="FillAndExpand"
            SeparatorColor="Silver">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Padding="10">
                            <Label Text="{Binding Text}" FontSize="Medium" FontAttributes="Bold" LineBreakMode="TailTruncation"
                                TextColor="Black" VerticalOptions="Start" HorizontalOptions="StartAndExpand" />

                            <Label Text="{Binding SubText}" FontSize="Small" Margin="0, 5, 0, 0" LineBreakMode="TailTruncation"
                                TextColor="Gray" VerticalOptions="Start" HorizontalOptions="StartAndExpand" />                

                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

Код страницы сзади (вы можете выполнить рефакторинг в MVVM по мере необходимости) ...

using System;
using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace Playpen
{
    public partial class MainPage : ContentPage
    {
        public ObservableCollection<ListItem> DataSource { get; set; }

        public MainPage()
        {
            this.BindingContext = this;

            DataSource = new ObservableCollection<ListItem>();

            DataSource.Add(new ListItem() { Text = "Item 1", SubText = "Sub Item Text 1" });
            DataSource.Add(new ListItem() { Text = "Item 2", SubText = "Sub Item Text 2" });
            DataSource.Add(new ListItem() { Text = "Item 3", SubText = "Sub Item Text 3" });

            InitializeComponent();
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...