绑定自定义ListView控件Xamarin格式

kfgdxczn  于 2023-03-06  发布在  其他
关注(0)|答案(1)|浏览(130)

我有一个列表视图控件嵌入在我的主页。我有一个主页视图模型,是绑定到主页。我有一个列表视图在主页视图模型,我只是想把该列表传递给我的列表视图控件。
这是我的主页。

<?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:views="clr-namespace:ContentViewBinding.Views" 
         xmlns:contentviewbinding="clr-namespace:ContentViewBinding"
         xmlns:ViewModels="clr-namespace:ContentViewBinding.ViewModels"
         x:Class="ContentViewBinding.Views.HomePage"
         x:Name="home"
         >

<ContentPage.BindingContext>
    <ViewModels:HomePageViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
  
    <StackLayout >
        <StackLayout HeightRequest="200">
            <views:ChildHomeView  Company="{Binding ., Source={x:Reference home}}" />
        </StackLayout>
        <StackLayout>
            <ListView ItemsSource="{Binding HomeList}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <Label Text="{Binding Title}" TextColor="Black"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </StackLayout>
</ContentPage.Content>

这是我的列表视图控件

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="ContentViewBinding.Views.ChildHomeView">
 <ContentView.Content>
  <StackLayout HeightRequest="300">
        <ListView ItemsSource="{Binding Company.HomeList}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding Title}" TextColor="Black"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
  </StackLayout>
</ContentView.Content>

请给予我一些解决办法,我不知道该怎么做

but5z9lq

but5z9lq1#

您可以使用以下代码:
ContentView.xaml:

<ContentView  
              ...
              x:Name="this">
    <StackLayout HeightRequest="300" 
                 BindingContext="{x:Reference this}">
        <ListView ItemsSource="{Binding NameIdList}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding Name}" TextColor="Black"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentView>

ContentView.xaml.cs:

public partial class ChildHomeView : ContentView
{
    public static readonly BindableProperty NameIdListProperty
        = BindableProperty.Create(nameof(NameIdList), typeof(IList<NameId>), typeof(ChildHomeView));
    public IList<NameId> NameIdList
    {
        get => (IList<NameId>)GetValue(ChildHomeView.NameIdListProperty);
        set => SetValue(ChildHomeView.NameIdListProperty, value);
    }
    public ChildHomeView()
    {
        InitializeComponent();
    }
}

名称ID类:

namespace ContentViewBinding.Models
{
    public class NameId
    {
        public string Name { get; set; }
        public string Id { get; set; }
    }
}

在您的主页中:

<ContentPage 
             ...
             xmlns:controls="clr-namespace:ContentViewBinding.CustomControls"
             xmlns:ViewModel="clr-namespace:ContentViewBinding.ViewModels">
    <ContentPage.BindingContext>
        <ViewModel:VM/>
    </ContentPage.BindingContext>
    <StackLayout>
        <controls:ChildHomeView NameIdList="{Binding NameIdList}"/>
    </StackLayout>
</ContentPage>

和VM.cs:

namespace ContentViewBinding.ViewModels
{
    public class VM 
    {
        public ObservableCollection<NameId> NameIdList { get; set; }
        
        public VM()
        {
            NameIdList = new ObservableCollection<NameId> 
            { 
                new NameId { Id = "Id A", Name = "Name A" }, 
                new NameId { Id = "Id B", Name = "Name B" }, 
                new NameId { Id = "Id C", Name = "Name C" } 
            };
        }
    }
}

相关问题