Reading offical Xamarin documentation about listview, you create template in xaml (xml file) and fill listView items programably in code:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:constants="clr-
namespace:XamarinFormsSample;assembly=XamarinFormsXamlSample"
x:Class="XamarinFormsXamlSample.Views.EmployeeListPage"
Title="Employee List">
<ListView x:Name="EmployeeView">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding DisplayName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
And then you fill items in code like this:
private ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
public EmployeeListPage()
{
// just for demonstration purposes
employees.Add(new Employee{ DisplayName="Rob Finnerty"});
employees.Add(new Employee{ DisplayName="Bill Wrestler"});
//defined in XAML to follow
EmployeeView.ItemsSource = employees;
...
}
Is there a way to define items in xaml? I'm setting up a simple settings page and would like to add items in xml. I ended up using TableView, but am still curious if it can be done?
You can define ListView.Items and place some other WPF controls in there.
<ListView>
<ListView.Items>
<Label Content="123"/>
<TextBox Text="ABC"/>
<CheckBox Checked="True"/>
</ListView.Items>
</ListView>
You can define the ItemSource in the xaml by putting the list items inside of some kind of ObservableCollection.
<?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:col="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:models="clr-namespace:UserApp.Models"
x:Class="UserApp.Interface.SettingsPage"
Title="Settings">
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding DisplayName}" />
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsSource>
<col:ArrayList>
<models:Employee DisplayName="Rob Finnerty" />
<models:Employee DisplayName="Bill Wrestler" />
</col:ArrayList>
</ListView.ItemsSource>
</ListView>
</ContentPage>
You can also use <x:String>text</x:String> instead of objects by removing the property name from the binding.
Related
I have BindableLayout, that binded to ViewModel property (which is actually a subclass), but inside this layout i'm trying to bind Command to VM another property. How can i specify property for binding in this case?
XAML code:
<Grid BindableLayout.ItemsSource="{Binding ActualValues}"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="0">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Frame Margin="{Binding Margin}">
<Entry TabIndex="{Binding Id}"
Text="{Binding Value}"
Placeholder="{Binding Placeholder}"
Style="{StaticResource EntryStyle}">
<Entry.Behaviors>
<behaviors:EntryCompletedBehavior Command="{Binding EntryCompletedCommand}"/>
</Entry.Behaviors>
</Entry>
</Frame>
</DataTemplate>
</BindableLayout.ItemTemplate>
</Grid>
You could set the binding path in xaml
in 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:xxx"
x:Class="xxx.MainPage"
x:Name="page" // set name of ContentPage
>
Command="{Binding Source={x:Reference page} , Path=BindingContext.xxxCommand}"
xxxCommand is an ICommand which been defined in VM .
I am not sure why I am getting this error I have read alot about this error and sounds like it is related to the title in the xaml file not being set. All my pages have the title set. The error is coming from the app.xaml.cs MainPage= new MainPage();
is there something else that can trigger this exeception?
System.InvalidOperationException: Master and Detail must be set before adding MasterDetailPage to a container
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:ReimbursementApp.Pages"
x:Class="ReimbursementApp.Pages.MainPage"
xmlns:telerikPrimitives="clr-namespace:Telerik.XamarinForms.Primitives;assembly=Telerik.XamarinForms.Primitives"
Title="Main">
<MasterDetailPage.Master>
<pages:MenuPage />
</MasterDetailPage.Master>
</MasterDetailPage>
MenuPage.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"
x:Class="ReimbursementApp.Pages.MenuPage"
xmlns:telerikPrimitives="clr-namespace:Telerik.XamarinForms.Primitives;assembly=Telerik.XamarinForms.Primitives"
xmlns:telerik="clr-namespace:Telerik.XamarinForms.DataControls;assembly=Telerik.XamarinForms.DataControls"
Title="Menu">
<!--<ContentPage.ToolbarItems>
<ToolbarItem Activated="OnToolbarButtonClick" Order="Primary" Priority="0" Icon="hamburgerButtonIcon"/>
</ContentPage.ToolbarItems>
<Grid>
<telerikPrimitives:RadSideDrawer x:Name="drawer" DrawerLength="250">
</telerikPrimitives:RadSideDrawer>
</Grid>-->
<StackLayout VerticalOptions="FillAndExpand">
<ListView x:Name="ListViewMenu"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10">
<Label Text="{Binding Title}" FontSize="20"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
The error message is literally saying what's wrong:
Master and Detail must be set before adding MasterDetailPage to a container
You already have set something in the Master property, like this:
<MasterDetailPage.Master>
<pages:MenuPage />
</MasterDetailPage.Master>
But you should also set the Detail property, something like this:
<MasterDetailPage.Detail>
<pages:MyMainPage />
</MasterDetailPage.Detail>
The MasterDetailPage is only a container and must have both a master (usually a menu) and a detail (something picked from the menu) to show something useful.
I have also seen this error occur if the page designated as the MasterDetailPage does not load correctly i.e. there may be errors in your xaml on that page or even errors found loading a static resource specified in your App.xaml. This was the case for me and fixing the errors resolved my problem.
I have a list of two names placed in the code behind that are binded to a ListView on the xaml file, and after trying to change the name colors to white, I got this on the screen:
Screenshot of the list
the code: 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" x:Class="RoseySports.Invite_Page">
<ContentPage.Content>
<AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Image AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
Source="background.jpg" Aspect="AspectFill"/>
<ScrollView AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1">
<ContentView Padding="10, 40, 10, 10">
<ListView x:Name="inviteesp" SeparatorColor="White" BackgroundColor="Transparent" Header="People">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding names}" TextColor="White"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentView>
</ScrollView>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
C# (Code-Behind):
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace RoseySports
{
public partial class Invite_Page : ContentPage
{
public Invite_Page()
{
InitializeComponent();
var names = new List<String>
{
"Mohamed Al-Sabbagh",
"Adrien Tardy"
};
inviteesp.ItemsSource = names;
}
}
}
After adding the binding source to the ListView property
Since you are using a Header which is not shown, but this strange text, I assume the problem is connected with that.
Are you sure you have all NuGet packages and are on the correct NetStandard version?
You should probably check your targeting versions, clean, restore and rebuild your solution.
If this doesn't help try adding an explicit layout for your header like
<ListView x:Name="listView" Header="TEST HEADER"> <!-- this object will be the bindingcontext -->
<ListView.HeaderTemplate >
<DataTemplate>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding .}"/> <!-- use the object in Header as the binding source -->
</StackLayout>
</DataTemplate>
</ListView.HeaderTemplate>
<ListView.ItemTemplate>
your template goes here
please also check out this xamarin forums thread
https://forums.xamarin.com/discussion/126386/setvalue-can-not-convert-system-readonlyspan-to-type-xamarin-forms-color
Maybe try to update to Xamarin Forms 3 if you haven't already.
Check if there are references to older Xamarin Forms versions in your projects
files (android, ios, ...).
Are you sure this is correct?
{Binding names}
Try with
{Binding .}
Your ListView's ItemsSource should be bound to names and inside of the template just use {Binding} to point at the element instead of property of that element.
<ListView x:Name="inviteesp" SeparatorColor="White" BackgroundColor="Transparent" Header="People" ItemsSource="{Binding names}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding}" TextColor="White"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Currently you're trying to bind each character to each TextCell instead of trying to bind each string to each TextCell.
I have a problem. In Datatemplate i cant reach TextCell in xaml. Anybody can help me?
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Spirocco.MedicationPage"
Title="Gyógyszerek">
<ContentPage.Content>
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
Update: I reach it but the VS dont show it.
In first answer there is a solution.
<StackLayout>
<ListView x:Name="Medications" ItemsSource="{Binding Medications}">
<ListView.ItemTemplate>
<DataTemplate>
<ImageCell ImageSource="{Binding Icon}" Text="{Binding Name}" Detail="{Binding Type}" Tapped="ImageCell_Tapped"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
I don't know why my list view is not showing up, I don't know if I need to add something, here is my code:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" BackgroundColor="#1B1B1B" x:Class="wantafood.Page1">
<ContentPage.Content>
<ListView x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="#eee"
Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Label Text="LabelText"
TextColor="#f35e20" />
<Label Text="LabelText2"
HorizontalOptions="EndAndExpand"
TextColor="#503026" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
If someone could point to me what I'm missing, I would really appreciate that, I'm sure is something dumb, but I can't see it, thanks.