0

I have a ListView

 <StackLayout Margin="12"
                 Padding="24"
                 BackgroundColor="White"
                 HorizontalOptions="Center"
                 VerticalOptions="Center">
        <StackLayout >
        <ListView x:Name="IzberiFirmaListView" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                        <ViewCell>
                        <Label TextColor="Black" Text="{Binding Title}"></Label>
                        </ViewCell>
                    </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
        <StackLayout Orientation="Horizontal">
        <Button Text="Save"  BackgroundColor="#76C3D7" TextColor="White" Clicked="OnSave"></Button>
        <Button Text="Cancel"  BackgroundColor="Red" TextColor="White" Clicked="OnCancel"></Button>
        </StackLayout>

MY class have two parameters Id and Title. In my scenario user must select one item from the list view and then when press on save button pass choosen Id from the class. Any suggestions how to organize?

Rajzer
  • 1,174
  • 11
  • 24
  • This may help you. https://stackoverflow.com/questions/59862593/how-to-show-data-from-listview-to-entry-in-xamarin-by-selecteditem-through-mvvm – Ganesan VG Jan 30 '20 at 11:17
  • Use `SelectedItem` property of ListView. Either bind it or use it in xaml.cs – Nikhileshwar Jan 30 '20 at 11:18
  • @GanesanVG the question you pointed is about changing a property on SelectionChange. Whereas this question is about using the already selected item in a button click. Though your answer in that question is great. I find it a little far fetched to this question. Just my opinion. – Nikhileshwar Jan 30 '20 at 11:35
  • Okay. Thanks for your opinion. Maybe I miss understood the question. – Ganesan VG Jan 30 '20 at 11:52

1 Answers1

1

Use the SelectedItem property of ListView.

using your x:Name of ListView in question

void Button_Clicked(System.Object sender, System.EventArgs e)
{
    var selectedItem = (IzberiFirmaListView.SelectedItem as YourModel);

    // your save operation
}
Nikhileshwar
  • 1,666
  • 1
  • 11
  • 26
  • In SelectedItem in xaml i add SelectedItem={Binding Id}? – Rajzer Jan 30 '20 at 11:56
  • Id is a Property in your ViewModel? Have you used commands for Button click. Please provide code behind (xaml.cs) file if possible also change the Xaml in your question to make it clear. – Nikhileshwar Jan 30 '20 at 12:00
  • its working with button clicked. i will try to change with command on the button to follow MVVM patter. Thanks – Rajzer Jan 30 '20 at 12:04
  • 1
    On using MVVM use the ViewModel Property binded with the SelectedItem. – Nikhileshwar Jan 30 '20 at 12:05
  • i Created in viewModel property of type int Id. In ListView i added SelectedItem{Binding Id}. Added BindingContext of the viewModel. Now in the viewModel how to pass that parameter Id? To call an action? – Rajzer Jan 30 '20 at 12:16
  • What is your ListView ItemsSource i hope it is a collectoin of int? If you have a property Id in your ViewModel and going to use Command. Go ahead and use that property it makes sense. If you don't want that for some reason check this [question](https://stackoverflow.com/questions/25912091/how-do-i-pass-the-button-as-commandparameter-from-xaml-in-a-xamarin-forms-page). Please update your question with xaml.cs it really needs to be added. – Nikhileshwar Jan 30 '20 at 12:22
  • In my modelView I have a list of Firms with title and Id. I pass that with bindingContext to the listView ItemSource={Binding UserFirms} and List is showing. When user select firm and press Save button i need to send that id to the async Task SelectFirm(int id) in the ViewModel to make some action in the api – Rajzer Jan 30 '20 at 12:37
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206922/discussion-between-nikhileshwar-and-vasko-josifovski). – Nikhileshwar Jan 30 '20 at 12:51