如何在ViewCell中绑定数据网格

时间:2018-09-21 07:31:08

标签: xaml listview xamarin xamarin.forms datagrid

我正在实现无限滚动的数据网格,为此我正在将数据网格添加到Listview ViewCell。我的xaml就像

 <ListView ItemsSource="{Binding Items}" CachingStrategy="RecycleElement" HasUnevenRows="True">
            <ListView.Behaviors>
                <extended:InfiniteScrollBehavior IsLoadingMore="{Binding IsBusy}" />
            </ListView.Behaviors>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <datagrid:DataGrid ItemsSource="{Binding Items}" Margin="5" RowHeight="30" HeaderHeight="30" BorderColor="#CCCCCC" HeaderBackground="#5b9bd5" HeaderTextColor="White">
                            <datagrid:DataGrid.Columns>
                                <datagrid:DataGridColumn Title="Job#" PropertyName="Job" Width="0.5*"/>
                                <datagrid:DataGridColumn Title="Source" PropertyName="Source" Width="0.5*"/>
                                <datagrid:DataGridColumn Title="Location" PropertyName="Location"  Width="0.5*"/>
                                <datagrid:DataGridColumn Title="Service" PropertyName="Service"  Width="0.5*"/>
                                <datagrid:DataGridColumn Title="Assignee" PropertyName="Assignee"  Width="0.5*"/>
                                <datagrid:DataGridColumn Title="Service Date" PropertyName="ServiceDate" Width="0.5*"/>
                                <datagrid:DataGridColumn Title="Time Slot" PropertyName="TimeSlot" Width="0.5*"/>
                                <datagrid:DataGridColumn Title="Service Status" PropertyName="ServiceStatus" Width="0.5*"/>
                            </datagrid:DataGrid.Columns>
                            <datagrid:DataGrid.RowsBackgroundColorPalette>
                                <datagrid:PaletteCollection>
                                    <Color>#d2deef</Color>
                                    <Color>#eaeff7</Color>
                                </datagrid:PaletteCollection>
                            </datagrid:DataGrid.RowsBackgroundColorPalette>
                        </datagrid:DataGrid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.Footer>
                <Grid Padding="6" IsVisible="{Binding IsBusy}">
                    <Grid.Triggers>
                        <Trigger TargetType="Grid" Property="IsVisible" Value="False">
                            <Setter Property="HeightRequest" Value="0" />
                        </Trigger>
                    </Grid.Triggers>
                    <Label Text="Loading..." TextColor="DeepPink" FontSize="20" FontAttributes="Bold" VerticalOptions="Center" HorizontalOptions="Center" />
                </Grid>
            </ListView.Footer>
        </ListView>

我的ViewModel就像

public class MainViewModel : INotifyPropertyChanged
{
    private bool _isBusy;
    private const int PageSize = 25;
    public InfiniteScrollCollection<Jobs> Items { get; }

    public bool IsBusy
    {
        get { return _isBusy; }
        set
        {
            _isBusy = value;
            OnPropertyChanged(nameof(IsBusy));
        }
    }


    #region fields
    private List<Jobs> jobs;
    private Jobs selectedItem;
    private bool isRefreshing;
    #endregion

    #region Properties
    public List<Jobs> Jobs
    {
        get { return jobs; }
        set { jobs = value; OnPropertyChanged(nameof(jobs)); }
    }

    public Jobs SelectedJob
    {
        get { return selectedItem; }
        set
        {
            selectedItem = value;
            System.Diagnostics.Debug.WriteLine("Job Selected : " + value?.Job);
        }
    }

    public bool IsRefreshing
    {
        get { return isRefreshing; }
        set { isRefreshing = value; OnPropertyChanged(nameof(IsRefreshing)); }
    }

    public ICommand RefreshCommand { get; set; }
    #endregion

    public MainViewModel()
    {
        Jobs = new List<Jobs>();
        for (int i = 0; i <= 1000; i++)
        {
            Jobs.Add(new Jobs { Job = i, Source = "Source" + i, Location = "Loaction" + i, Service = "Service" + i, Assignee = "Assignee" + i, ServiceDate = "ServiceDate" + i, TimeSlot = "TimeSlot" + i, ServiceStatus = "ServiceStatus" + i });
        }

        Items = new InfiniteScrollCollection<Jobs>
        {
            OnLoadMore = async () =>
            {
                IsBusy = true;

                // load the next page
                var page = Items.Count / PageSize;

                var items = await GetItemsAsync(page, PageSize);

                IsBusy = false;

                // return the items that need to be added
                return items;
            },
            OnCanLoadMore = () =>
            {
                return Items.Count < Jobs.Count;
            }
        };

        DownloadDataAsync();
    }

    private async Task DownloadDataAsync()
    {
        var items = await GetItemsAsync(pageIndex: 0, pageSize: PageSize);

        Items.AddRange(items);
    }
    public async Task<List<Jobs>> GetItemsAsync(int pageIndex, int pageSize)
    {
        await Task.Delay(5000);

        return Jobs.Skip(pageIndex * pageSize).Take(pageSize).ToList();
    }

    private async void CmdRefresh()
    {
        IsRefreshing = true;
        // wait 3 secs for demo
        await Task.Delay(3000);
        IsRefreshing = false;
    }

    #region INotifyPropertyChanged implementation
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }

    #endregion
}

每次我的单元格中显示25个项目时,谁能帮助我如何仅在一个单元格中加载数据,并且每次在以前的数据下方添加额外的数据。

1 个答案:

答案 0 :(得分:0)

为您的ListView命名并更改

<datagrid:DataGrid ItemsSource="{Binding Path=BindingContext, Source={x:Reference Name=<NameOfListView>}}"