Reputation: 945
I have a List of strings in my main page, which I want to pass to another view. The attribute, to whom I writing to has a binding. Essentially it's the ItemSource of the ListView.
I tried additionally to List, both string[] and ObservableCollection as data storage for Projects, since the documentation recommends to use it.
Trying to use the -element from MyView.xaml in MainPage.xaml works like charm.
MainPage.xaml:
Content = new MyView { Projects = MyProjects };
MyView.xaml.cs:
public partial class MyView : ContentView
{
private List<string> _projects;
public List<string> Projects
{
get => _projects;
set
{
_projects = value;
OnPropertyChanged(nameof(Projects));
}
}
//public string[] Test = new string[] { "Test", "Hallo", "Welt" };
public MyView ()
{
InitializeComponent();
}
}
MyView.xaml:
<StackLayout>
<ListView BackgroundColor="Red" x:Name="p_lstView" RowHeight="160" SeparatorColor="DodgerBlue" ItemsSource="{Binding Projects}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding}"></Label>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Upvotes: 0
Views: 106
Reputation: 523
You have some issues with the BindingContext
on the ListView.ItemSource
. The current context is on the MainPage.
You need to change:
<ListView BackgroundColor="Red" x:Name="p_lstView" RowHeight="160" SeparatorColor="DodgerBlue" ItemsSource="{Binding Content.Projects}">
Alternative, you can set the BindingContext
in the MyView
constrctor.
Upvotes: 1
Reputation: 15816
You should set the BindingContext
then the binding will work:
public MyView()
{
InitializeComponent();
BindingContext = this;
}
Upvotes: 1