ViewCell repeated many times in Listview Xamarin - xamarin

I have
Test.xaml
<ContentPage.Content>
<StackLayout>
<ListView x:Name="myCarts" HasUnevenRows="true">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label TextColor="#848484" Text="11" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
Test.xaml.cs
public Test()
{
InitializeComponent();
var cartUserInfo = Preferences.Get("CartUser", "defaultcart");
myCarts.ItemsSource = cartUserInfo;
}
I checked the data in cartUserInfo Preferences only 1 line of data. Why does it repeat again in the UI?
How does it display correctly. Thank you

Related

How To Display list view item pop up in xamarin form

Hello Developer I want to display pop in my page. The given image link red portion I want to display please help me out how can I do that in xamarin form
Hello Xamarin developer I have attach one image link in this question .This image red mark portion I want display on my page how can I do that
Not sure what is the content of your ListView, but here's the simple example of pop up with ListView in it, so you can adapt it as you like.
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="TestPopUp.MainPage">
<AbsoluteLayout Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Button Text="Pop up!" VerticalOptions="CenterAndExpand" HorizontalOptions="Center" Clicked="Button_Clicked" />
</StackLayout>
<ContentView x:Name="popupView" BackgroundColor="Transparent" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<Frame CornerRadius="10" Padding="0" BorderColor="LightGray">
<StackLayout Orientation="Vertical" HeightRequest="300" WidthRequest="200" BackgroundColor="White">
<ListView SeparatorVisibility="None" ItemsSource="{Binding MyList}" VerticalScrollBarVisibility="Never">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding .}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</Frame>
</StackLayout>
</ContentView>
</AbsoluteLayout>
</ContentPage>
C#:
public partial class MainPage : ContentPage
{
public ObservableCollection<string> MyList { get; set; } = new ObservableCollection<string>();
public MainPage()
{
InitializeComponent();
MyList.Add("Item 1");
MyList.Add("Item 2");
MyList.Add("Item 3");
MyList.Add("Item 4");
MyList.Add("Item 5");
MyList.Add("Item 6");
MyList.Add("Item 7");
BindingContext = this;
}
private void Button_Clicked(object sender, EventArgs e)
{
popupView.IsVisible = true;
}
}
This will give you output like this:
So, when you want to show this pop up, just put popupView.IsVisible = true in code behined.

xamarin collectionview cell custom border

How to add custom border to the collectionview cell like the image?
<CollectionView HeightRequest="100" ItemsSource="{Binding Test}">
<CollectionView.ItemsLayout >
<GridItemsLayout Orientation="Vertical" Span="4"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid >
<Image Source="{Binding Image}" ></Image>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Put the collection view into a colored container could do that, and set margin for the collection view as the width of border, like
<StackLayout BackgroundColor="Black" HorizontalOptions="FillAndExpand">
<CollectionView Margin="4,0">
</CollectionView>
</StackLayout>
I use a sub-class frame to add border, please take a look at the following code:
Creating custom frame in .Net standard library project(shared code).
public class MyFrame : Xamarin.Forms.Frame
{
}
Custom renderer in Android platform.
[assembly: ExportRenderer(typeof(MyFrame), typeof(MyFrameRenderer))]
namespace demo3.Droid
{
[Obsolete]
class MyFrameRenderer : VisualElementRenderer<Frame>
{
public MyFrameRenderer()
{
SetBackgroundDrawable(Resources.GetDrawable(Resource.Drawable.blue_rect));
}
}
}
<ContentPage.Content>
<StackLayout HorizontalOptions="FillAndExpand">
<control:MyFrame Padding="5">
<CollectionView BackgroundColor="Transparent" ItemsSource="{Binding Test}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical" Span="4" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame BackgroundColor="Transparent" BorderColor="Gray">
<Label Text="{Binding .}" TextColor="Black" />
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</control:MyFrame>
</StackLayout>
</ContentPage.Content>
The screenshot:

How to delete item from ListView in Xamarin forms

I have a ListView and I want to delete some items, I have not found a useful answer yet.
this is a XMAL :
<ListView.ItemTemplate >
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Name}"
Style="{DynamicResource ListItemTextStyle}" />
<Label Text="{Binding PhoneNo}"
Style="{DynamicResource ListItemDetailTextStyle}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
and listview :
public ObservableCollection<Contact> ContactList2 { get; set; }
I can easily add it, but I do not know how to delete it.
Use a context menu
XAML
<ViewCell.ContextActions>
<MenuItem Clicked="OnDelete" CommandParameter="{Binding .}"
Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
code behind
public void OnDelete (object sender, EventArgs e) {
var mi = ((MenuItem)sender);
Contact contact = (Contact)mi.CommandParameter;
ContactList2.Remove(contact);
}

Binding Xamarin XAML to code behind properties (ListView)

I want to bind my code-behind property, which is a list, to a XAML ListView. However, my ListView is never populated and I have no idea why.
<?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="BLEPl.EvaluationPage"
Title="Evaluation">
<ContentPage.Content>
<StackLayout Padding="0,20,0,20">
<ListView HasUnevenRows="True" IsVisible="True" x:Name="TableLayout" ItemsSource="{Binding Path=SensorValues}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Style="{StaticResource TitleLabel}" Grid.Column="0" Grid.Row="0" Text="Zeitstempel: " />
<Label Style="{StaticResource DataLabel}" Grid.Column="1" Grid.Row="0" Text="{Binding TimeStamp}" />
<Label Style="{StaticResource TitleLabel}" Grid.Column="0" Grid.Row="1" Text="Wert: " />
<Label Style="{StaticResource DataLabel}" Grid.Column="1" Grid.Row="1" Text="{Binding Value}" />
<Label Style="{StaticResource TitleLabel}" Grid.Column="0" Grid.Row="2" Text="Sensor: " />
<Label Style="{StaticResource DataLabel}" Grid.Column="1" Grid.Row="2" Text="{Binding Sensor.Name}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class EvaluationPage : ContentPage
{
private ObservableCollection<SensorValue> SensorValues = new ObservableCollection<SensorValue>();
public EvaluationPage ()
{
InitializeComponent ();
using (var context = new BlepContext(true))
{
var repo = new BLEPRepository(context);
SensorValues = new ObservableCollection<SensorValue>(repo.GetAllSensorValues());
}
}
}
When I run this, my ListView doesn't contain any items. I do not get errors and my app compiles fine. Am I doing something wrong here?
<ListView HasUnevenRows="True" IsVisible="True" x:Name="TableLayout" ItemsSource="{Binding SensorValues}">
you can only bind to public properties, and you need to set a BindingContext for the page
// needs to be a PUBLIC PROPERTY
public ObservableCollection<SensorValue> SensorValues { get; set; }
public EvaluationPage ()
{
InitializeComponent ();
// set the BindingContext
this.BindingContext = this;
using (var context = new BlepContext(true))
{
var repo = new BLEPRepository(context);
SensorValues = new ObservableCollection<SensorValue>(repo.GetAllSensorValues());
}
}

Xamarin - how to open a new ContentPage in a ListView?

I have a list that comes from my web service and i display it like so:
<StackLayout HorizontalOptions="FillAndExpand">
<ListView x:Name="curso" ItemSelected="CursoView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout Orientation="Horizontal">
<StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
<Label StyleClass="Header" Text="{Binding name}" />
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
And now i want to be able to click on an item and open a new ContentPage
public void CursoView_ItemSelected(object sender, ItemSelectedEventArgs e)
{
// this assumes your current page is already contained in a NavigationPage
Navigation.PushAsync(new MyNextPage());
}

Resources