How to add a button only once in a List View xamarin Forms - xaml

Using this code
x:Class="Fit_Plans.TimelinePage"
Title="Timeline"
BackgroundColor="{ DynamicResource MainWrapperBackgroundColor }
<ContentPage.Content>
<ListView
ItemsSource="{ Binding TimelineList }"
SeparatorVisibility="None"
BackgroundColor="{ DynamicResource BasePageColor }"
HasUnevenRows="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<StackLayout>
<Grid x:Name="GridTasks">
<local:TimelineItemTemplate
Padding="{ DynamicResource MainWrapperPadding }"/>
</Grid>
</StackLayout>
<artina:Button
Clicked="OnNextButtonTapped"
Style="{DynamicResource PrimaryActionButtonStyle}"
Text="Order"
VerticalOptions="EndAndExpand"
WidthRequest="{ artina:OnOrientationDouble
LandscapeTablet=600 }"
HorizontalOptions="{ artina:OnOrientationLayoutOptions
PortraitPhone=Fill,
LandscapePhone=Fill,
PortraitTablet=Fill,
LandscapeTablet=Center }"/>
<StackLayout>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
Result :
But if I want to display the order Button only 1 time at the end of the list view , how to achieve that?
I have tried this with no success :
<ContentPage.Content>
<ListView
ItemsSource="{ Binding TimelineList }"
SeparatorVisibility="None"
BackgroundColor="{ DynamicResource BasePageColor }"
HasUnevenRows="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<StackLayout>
<Grid x:Name="GridTasks">
<local:TimelineItemTemplate
Padding="{ DynamicResource MainWrapperPadding }"/>
</Grid>
</StackLayout>
<StackLayout>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
<artina:Button
Clicked="OnNextButtonTapped"
Style="{DynamicResource PrimaryActionButtonStyle}"
Text="Order"
VerticalOptions="EndAndExpand"
WidthRequest="{ artina:OnOrientationDouble
LandscapeTablet=600 }"
HorizontalOptions="{ artina:OnOrientationLayoutOptions
PortraitPhone=Fill,
LandscapePhone=Fill,
PortraitTablet=Fill,
LandscapeTablet=Center }"/>
It displayed the button at the end but with an empty list

ListView provides HeaderTemplate and FooterTemplate. You should put your button inside FooterTemplate as follows:
<ListView ItemsSource="{ Binding TimelineList }" SeparatorVisibility="None" BackgroundColor="{ DynamicResource BasePageColor }" HasUnevenRows="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
.................
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.FooterTemplate>
<DataTemplate>
<ContentView>
//Put your button here
</ContentView>  
</DataTemplate>  
</ListView.FooterTemplate>
</ListView>

As of previous answer you can put in in footer or if you'd want to keep he button inside the cell template then wrap the button in with a IsVisible = your custom converter, that would check if (myIndex == totalListCount) then the button would be displayed for the last item in list only.

Related

Xamarin XAML file wont change after changing some code

In my XAML File after changing some code inside it and then debug on my phone, the changes I made do not reflect on my phone. It keep showing my old code.
Old code :
<ListView
ItemsSource="{Binding Allgroups}"
RowHeight="75"
IsGroupingEnabled="true">
<ListView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding NameNative}"></Label>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding TitleWithItemCount}" />
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
</ListView>
new code :
<Label Text="HELOO WORLD "></Label>
I Noticed every time i run the code , it keep showing the list.I have to click the save button then the list disappear and hello world appear .
I reset VS settings and relaunched it .
worked for me
<CollectionView ItemsSource="{Binding Categoriesxx}" IsGrouped="True">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="1" VerticalItemSpacing="5" HorizontalItemSpacing="5"></GridItemsLayout>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout >
<Label Text="{Binding NameNative}"></Label>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
<CollectionView.GroupHeaderTemplate>
<DataTemplate>
<StackLayout BackgroundColor="AliceBlue">
<Label Text="{Binding Name}" FontSize="Title"></Label>
<Button Image="{Binding StateIcon}"
BackgroundColor="Transparent"
BorderColor="Transparent"
BorderWidth="0"
Command="{Binding BindingContext.ExpandCommand ,Source={x:Reference ServiceListPage}}"/>
</StackLayout>
</DataTemplate>
</CollectionView.GroupHeaderTemplate>
</CollectionView>

how to add checkbox in listview using xamarin forms

i want to create a checkbox in listview using xaml, after selecting the multiple checkbox click button to get all selected value.My code is :
<ContentPage.Content>
<ListView x:Name="ProductsListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Spacing="0">
<Label Text="{Binding retail_modified_item_id}"
TextColor="Blue" FontSize="13" />
<Label Text="{Binding name, StringFormat='Name :
{0:N}'}" TextColor="black" FontSize="10" />
<Label Text="{Binding retail_price,
StringFormat='Price : {0:N}'}"
TextColor="black" FontSize="10" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
my output is;
output without checkbox
i tried some sample code from https://www.ignatiuz.com/blog/xamarin/button-check-box-with-list-item-in-xamarin-forms-listview/
but i'm getting input:checkbox not found
i am new to xamarin please give some sample code or link to do this
You can use Plugin.InputKit from Nuget.
Usage
<?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"
xmlns:input="clr-namespace:Plugin.InputKit.Shared.Controls;assembly=Plugin.InputKit"
x:Class="xxx.MainPage">
<ContentPage.Content>
<ListView x:Name="ProductsListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Spacing="0">
<Label Text="{Binding retail_modified_item_id}"
TextColor="Blue" FontSize="13" />
<Label Text="{Binding name, StringFormat='Name :
{0:N}'}" TextColor="black" FontSize="10" />
<Label Text="{Binding retail_price,
StringFormat='Price : {0:N}'}"
TextColor="black" FontSize="10" />
<input:CheckBox Text="xxx" Type="Check"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
I have written a content on implementing the multiselect listview in xamarin forms. Please check this : https://androidwithashray.blogspot.com/2018/03/multiselect-list-view-using-xamarin.html
Hope this helps.
Add/remove value to local collection using OnCheckChanged method.
I used this way:
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="Some item" />
<CheckBox
IsChecked="true"
CheckedChanged="OnCheckedChanged"
HorizontalOptions="EndAndExpand"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
If you need to have checkboxes inside a listview, you can use Xamarin.Forms.InputKi (https://github.com/enisn/Xamarin.Forms.InputKit)
Then I would define it like this inside a DataTemplate's ViewCell:
<input:CheckBox Text="Hello World I'm Option 2" Type="Check"/>
There are also Bindable properties and events which you can use depending on your scenario:
CheckChanged: (Event) Invokes when check changed.
CheckChangedCommand: (Command) Bindable Command, executed when check
changed.
Key: (int) A key you can set to define checkboxes as ID.
Text: (string) Text to display description
IsChecked: (bool) Checkbox checked situation. Works TwoWay Binding as
default.
Color: (Color) Color of selected check.
TextColor: (Color) Color of description text.
Type: (CheckType) Type of checkbox checked style.
(Check,Cross,Star,Box etc.)

How to create list view with side count like mail

Am working small Xamarin.forms application.Now i have doubt to how to count and display list items in label
like email and gmail.
I'm browsing in google various sites i did't get result
My Xaml code is below how to display count in side bar and i used grid for that sum Xaml.task error will be occur
<?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:emit="clr-namespace:System.Reflection.Emit;assembly=mscorlib"
x:Class="vMonitor.Views.HomeListView"
Title="Home List View"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
<ScrollView>
<StackLayout Orientation="Vertical">
<ListView ItemsSource="{Binding Model}" Margin="20,0,20,0"
x:Name="listView"
SeparatorVisibility="Default" SeparatorColor="Brown"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<StackLayout Orientation="Horizontal" Padding="10">
<Label Text="{Binding Name}" FontSize="20" FontFamily="TimesNewRomen" FontAttributes="None">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding Path=BindingContext.TabCommand, Source={x:Reference listView}}" CommandParameter="{Binding .}"/>
</Label.GestureRecognizers>
</Label>
<Label Text="{Binding UserID}" FontSize="20" FontFamily="TimesNewRomen" FontAttributes="None"/>
</StackLayout>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ScrollView>
</ContentPage>
Then you can use ViewCell for List Item.
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="15,10" Orientation="Horizontal">
<Label
VerticalTextAlignment="Center"
Text="{Binding ItemName}" TextColor="#FF1654" FontAttributes="Bold" FontSize="16"/>
<Label VerticalOptions="End"
VerticalTextAlignment="Center"
Text="{Binding ItemCount}" TextColor="#FF1654" FontAttributes="Bold" FontSize="16"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
You can Bind ItemName and ItemCount. If you want to Display Left Side Image then you can add Image at start position in ViewCell.
For Expandable you have to use IsVisible Property for that cell.

How to set margin around ImageCell in Xamarin.Forms?

I want to have some space around the Image cell items of a ListView:
<StackLayout>
<ListView ItemsSource="{Binding Items}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ImageCell Text="{Binding Title}" ImageSource="{Binding Image}" TextColor="Black" DetailColor="Gray"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
but there's no Margin property available for the ImageCell?
EDIT
I did what cvanbeek suggested before posting the question:
<DataTemplate>
<StackLayout Padding="5">
<ImageCell Text="{Binding Title}" ImageSource="{Binding Image}" TextColor="Black" DetailColor="Gray"/>
</StackLayout>
</DataTemplate>
but I got this exception:
Unhandled Exception:
System.InvalidCastException: Specified cast is not valid.
in the OnCreate method in MainActivity.cs
DataTemplate definition
Only View can be put inside DataTemplate, however StackLayout is Layout.
Use a ViewCell instead , it is more flexible and controllable.
<ListView >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="20" Orientation="Horizontal" >
<Label Margin="20" Text="123"/>
<Image Source="Assets/StoreLogo.png"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

XAML background color and text color not changing

I do not know if my code displays a list of FOOS .It does generate a list item that is clickable which goes to the correct Details page, the problem is the list item is either blank or all the text is white. I cannot figure out which.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MaopApplication.Views.PipelineSearch">
<StackLayout Orientation="Vertical">
<StackLayout Orientation="Horizontal" >
<Entry Placeholder="Search With PipelineName" Text="{Binding SearchText}" ></Entry>
<Button Text="Search" Command="{Binding SearchPipes}" />
</StackLayout>
<ListView ItemsSource="{Binding Foos}" ItemTapped="OnListViewItemTapped" ItemSelected="OnListViewItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View BackgroundColor="Red">
<StackLayout>
<label Text="wtf" TextColor="Black"></label>
<label Text="{Binding Test}"TextColor="Black"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
CODE TO CREATE FOOS
var Foos = new List<object>() {new {Test = "foo"}, new {Test = "bar"}};
A couple things wrong here (maybe it was just a bad copy/paste job):
ViewCell.View does not have a BackgroundColor property, use StackLayout.BackgroundColor instead
label should be Label (capitals matter)
When I made those change to your code, it ran fine for me:
<ViewCell>
<ViewCell.View>
<StackLayout BackgroundColor="Red">
<Label Text="wtf" TextColor="Black"/>
<label Text="{Binding Test}" TextColor="Black"/>
</StackLayout>
</ViewCell.View>
</ViewCell>