我的主页中嵌入了一个列表视图控件。我有一个绑定到主页的主页视图模型。我在主页视图模型中有一个列表视图,我只想将该列表传递给我的列表视图控件。
这是我的主页。
<?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>
请给我一些我不知道该怎么做的解决方案
您可以使用以下代码:
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();
}
}
NameId类:
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" }
};
}
}
}