Passing data From ListView Item to next Form - c#

I know how to send data from an Entry, Label to the next Form , but how can i do this with a ListView.
I want a simple direct approach.
My XAML code :
<ListView x:Name="MyClientslistView" HasUnevenRows="true" ItemTapped="OnSalesReport">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" VerticalOptions="Center" Margin="5" >
<Label Text="{Binding client_name}" TextColor="#A7A392" />
<Label Text="{Binding client_address}" TextColor="Black" />
<Label Text="{Binding client_phone}" TextColor="Black" />
<Label Text="{Binding client_email}" TextColor="Black" />
<Label Text="{Binding AssignStatus}" TextColor="Black" />
<Label Text="{Binding assignmentComments}" TextColor="Black" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I already have loaded data from JSON to the ListView , now currently i want to move each ID on he Item to the Next Form , how can i tackle that ?

You can get the item from ItemTapped listener. In your case use below solution.
void OnSalesReport(object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
YourItemModel item = (YourItemModel)((ListView)sender).SelectedItem;
((ListView)sender).SelectedItem = null;
Navigation.PushAsync(new NextPage(item));
}

Sorry for the poor english
You can have your entire item at your event handler private void OnSalesReport(object sender, ItemTappedEventArgs e) through e.Item.
You must do var item = ((YourTypeClassItem)e.Item); then you get things like item.client_name and probably item.client_id.
I hope it helps you.

Related

Collect the identifier of a Button in a row of a CollectionView in xamarin forms

I am developing an application in xamarin forms and I have problems accessing the event of a button that I have in a CollectionView. For each button that is selected, depending on the row it is in, it will do a different thing with the id.
My xaml code with the CollectionView is as follows:
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="120" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="250" />
<ColumnDefinition Width="85" />
<ColumnDefinition Width="70" />
<ColumnDefinition Width="70" />
</Grid.ColumnDefinitions>
<Label Grid.Column="1"
Text="{Binding id_cliente}"
TextColor="black"
VerticalOptions="CenterAndExpand"
FontAttributes="Bold" />
<Label
Grid.Column="2"
Text="{Binding categoria}"
FontAttributes="Italic"
VerticalOptions="CenterAndExpand"
TextColor="black"/>
<Label Grid.Column="3"
Text="{Binding descripcion}"
TextColor="black"
VerticalOptions="CenterAndExpand"
FontAttributes="Bold" />
<Label
Grid.Column="4"
Text="{Binding estado}"
TextColor="Yellow"
VerticalOptions="CenterAndExpand"
FontAttributes="Bold" />
<Button Grid.Column="5" Text="Confirmar" BorderColor="#2b3c3c" BorderWidth="1" FontAttributes="Bold" BackgroundColor="#4ba862" TextColor="White" HeightRequest="60" Margin="0,0,0,0" x:Name="btnconfirmar" Clicked="ButtonConfirmacion_Clicked" Command="{Binding id_trabajo}"/>
<Button Grid.Column="6" Text="Asignar" BorderColor="#2b3c3c" BorderWidth="1" FontAttributes="Bold" BackgroundColor="#4ba862" TextColor="White" HeightRequest="60" Margin="0,0,0,0" x:Name="btnasignar" Clicked="ButtonListaTrabajos_Clicked"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
And in the cs code of that page I have:
private void ButtonConfirmacion_Clicked(object sender, EventArgs e)
{
Trabajo selectedItem1 = e.CurrentSelection[0] as Trabajo;
}
This code, when that row is selected, with an OnSelectionChanged (sender, e) method if it picks up the item which has been selected.
You could use the code below to get the x:Name of button in a row.
private void ButtonConfirmacion_Clicked(object sender, EventArgs e)
{
var button = sender as Button;
var elemets = (button.Parent as Grid).Children;
foreach (var item in elemets)
{
if (item.GetType().Name == "Button")
{
var btn_name = item.StyleId;
}
}
}

scroll problem when adding item to flowlistview in xamairn

i facing a problem when adding item dynamically to flowlistview the scroll get stuck for a bit
each time i press more the items get to the view but still there is a stuck in the scroll till i move the screen the scroll get the new heights
i facing a problem when adding item dynamically to flowlistview the scroll get stuck for a bit
each time i press more the items get to the view but still there is a stuck in the scroll till i move the screen the scroll get the new heights
private async void ContinueClick(object sender, EventArgs e)
{
var list1 = DataService.Instance.stockslist.Where(X => X.StockType == _Cat.StockTypeCode).ToList();
foreach (var item in list1)
{
li.Add(item);
}
MostNews.FlowItemsSource = li;
Device.BeginInvokeOnMainThread(() =>
MostNews.ScrollTo(li.Last(), ScrollToPosition.End, false));
// await Navigation.PushAsync(new SelectCreditCardPage(), true);
}
the xaml code
<controls:FlowListView
x:Name="MostNews"
BackgroundColor="{DynamicResource BackgroundColor}"
FlowColumnCount="2"
FlowColumnExpand="None"
HasUnevenRows="False"
RowHeight="280"
SeparatorVisibility="None"
VerticalOptions="StartAndExpand"
VerticalScrollBarVisibility="Never">
<controls:FlowListView.FlowColumnTemplate>
<DataTemplate>
<pancakeView:PancakeView
Margin="5"
Padding="5"
BackgroundColor="{DynamicResource ItemColor}"
CornerRadius="8"
Elevation="2"
HasShadow="False"
HeightRequest="250"
HorizontalOptions="FillAndExpand"
VerticalOptions="StartAndExpand"
WidthRequest="{x:OnIdiom Tablet=150,
Phone=130}">
<pancakeView:PancakeView.GestureRecognizers>
<TapGestureRecognizer Tapped="ProductDetailClick" />
</pancakeView:PancakeView.GestureRecognizers>
<StackLayout>
<forms:CachedImage
Aspect="AspectFit"
HeightRequest="150"
Source="{Binding Imgurl}" />
<Label
FontFamily="{DynamicResource VerdanaProRegular}"
Text="{Binding StockName}"
TextColor="{DynamicResource WhiteDarkFontColor}"
VerticalOptions="Start" />
<Label
FontFamily="{DynamicResource VerdanaProRegular}"
FontSize="10"
HorizontalOptions="Start"
Text="{Binding Freeze}"
TextColor="{DynamicResource DescLabelColor}"
VerticalOptions="Start" />
<Label
FontFamily="{DynamicResource VerdanaProBold}"
FontSize="25"
HorizontalOptions="EndAndExpand"
Text="{Binding Price, StringFormat='{0} ILS'}"
TextColor="{DynamicResource DarkLabelTextColor}"
VerticalOptions="EndAndExpand" />
</StackLayout>
</pancakeView:PancakeView>
</DataTemplate>
</controls:FlowListView.FlowColumnTemplate>
</controls:FlowListView>

Xamarin.Forms - Binding on ContextMenu

I have a list with songs as items. A long press on the element should display a context menu.
AllSongsViewModel.xaml:
<DataTemplate x:Key="SongTemplate">
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Edit" />
<MenuItem Text="Delete"/>
</ViewCell.ContextActions>
<StackLayout Padding="15,5" VerticalOptions="Center">
<Label Text="{Binding Title}"
FontSize="16"/>
<Label Text="{Binding Performer}"
FontSize="12"/>
</StackLayout>
</ViewCell>
</DataTemplate>
This works well, but I need to bind now so that the context menu opens depending on bool IsAdmin, which lies in AllSongsViewModel
AllSongsViewModel.cs:
public bool IsAdmin => _authService.LoggedUser.Role == "Admin";
But I don’t know how to bind this property to the context menu
Unfortunately you can't do this on your ViewModel. But you can set a BindingContextChange event on your View Cell and change it there like this:
XAML:
<DataTemplate x:Key="SongTemplate">
<ViewCell BindingContextChanged="OnBindingContextChanged">
<StackLayout Padding="15,5" VerticalOptions="Center">
<Label Text="{Binding Title}"
FontSize="16"/>
<Label Text="{Binding Performer}"
FontSize="12"/>
</StackLayout>
</ViewCell>
In your code behind:
private void OnBindingContextChanged(object sender, EventArgs e)
{
base.OnBindingContextChanged();
if (BindingContext == null)
return;
ViewCell theViewCell = ((ViewCell)sender);
var viewModel = this.BindingContext.DataContext as AllSongsViewModel;
theViewCell.ContextActions.Clear();
if (viewModel.IsAdmin)
{
theViewCell.ContextActions.Add(new MenuItem()
{
Text = "Delete",
});
theViewCell.ContextActions.Add(new MenuItem()
{
Text = "Edit",
});
}
}

Binding observable collection to listview doesn't pass items in Xamarin.iOS

I have a problem with binding observable collection to listview in Xamarin.iOS. My code works in Android and UWP project, but not in the iOS (in simulator).
I have a datatable with 4 columns and I'm creating observable collection with simple if from it:
DataTable dt = await CallWebServiceTemp(lines);
temperatureData = new ObservableCollection<object>();
if (dt != null)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
temperatureData.Add(dt.Rows[i]);
}
}
else
{
//code to deal with that
}
OnPropertyChanged(nameof(Temperature));
This observable collection is bound to ListView in XAML through Temperature:
public ObservableCollection<object> Temperature
{
get { return temperatureData; }
set { temperatureData = value; }
}
Listview is created in XAML like this:
<ListView ItemsSource="{Binding Temperature}" FlexLayout.AlignSelf="Auto">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" HorizontalOptions="Center">
<Label Text="{Binding ItemArray[0]}" HorizontalOptions="Center"/>
<Label Text="|" HorizontalOptions="Center"/>
<Label Text="{Binding ItemArray[1]}" HorizontalOptions="Center"/>
<Label Text="|" HorizontalOptions="Center"/>
<Label Text="{Binding ItemArray[2]}" HorizontalOptions="Center"/>
<Label Text="|" HorizontalOptions="Center"/>
<Label Text="{Binding ItemArray[3]}" HorizontalOptions="Center"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Now as I stated earlier, this works just fine in Andoird and UWP, but not in iOS. In there I get the right number of items in the listview, but no data passed through ItemArray in the label, they're empty.
Clearly I'm missing something.

Xamarin.Forms.Xaml.XamlParseException: Position 16:13. No Property of name Cliked found

I'm following a tutorial to make an app that receives and sends data to a web service.
But the time to create a button on the MainPage to bring the registration screen of the Visual Studio this error
Xamarin.Forms.Xaml.XamlParseException:. Position 16:13 In the Property of name Cliked found
and I lost because my this code like the tutorial.
Xaml:
<StackLayout Orientation="Vertical">
<Button Clicked="Btn_Clicked" Text = "test button" />
<ListView ItemsSource="{Binding ContactList}"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical" Padding="12,6">
<Label Text="{Binding Name}" FontSize="24"/>
<Label Text="{Binding Email}" FontSize="24"/>
<Label Text="{Binding City}" FontSize="24"/>
<Label Text="{Binding State}" FontSize="24"/>
<Label Text="{Binding Zip}" FontSize="24"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Xaml.cs
using System;
using Xamarin.Forms;
namespace ContactManagerApp.Views
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void Btn_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new NewContactPage());
}
}
}
Clicked is an event not a property so you cannot bind it. You should Bind Command property of Button. You should add a method into page.xaml.cs if you want to use Clicked event.
Xaml:
< Button Clicked="Btn_Clicked" Text = "test button" />
Xaml.cs
private void Btn_Clicked(object sender, EventArgs e)
{
//Do stuff
}

Categories