Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

MVVM: how to update an item inside a lstview with the text property of a button in the same lstview

$
0
0

Hi. Sorry if my English is rough, Spanish is my native language.

  1. I'm using MVVM.
  2. I have a collection of items inside a listview and each one has 2 buttons.
  3. Button.Text property is variable. It is the result of a convertion based on a property of the collection item.
  4. Both buttons use the same command.

I want to update the collection item with the random Button.Text generated in the convertion. How can I do that?

// =============================
//Model.cs

public class SomeClass 
{
    public string ItemType {get;set;}
    public string ButtonTextProperty {get;set;}
}
// =============================

// =============================
// ViewModel.cs

public Command SomeCommand { get; set; }

public ControlObjetosChecklistsViewModel()
{
    this.SomeCommand = new Command(async (object param) => await someCommand (param));
}

private async Task someCommand(object param)
{
    // get the object by taking the button bindingcontext
    // get button text property
    // update object with that text property
}
// =============================

// =============================
//View.xaml
<ListView x:Name="listView" ItemsSource="{Binding ItemSource}" SelectionMode="None" HasUnevenRows="True">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                        <Button x:Name="btn1" 
                                                Text="{Binding ItemType, Converter={StaticResource converter}, ConverterParameter=converterparameter1}" 
                                                Command="{Binding Source={x:Reference listView}, Path=BindingContext.SomeCommand}" 
                                                CommandParameter="{Binding .}" 
                                                HorizontalOptions="FillAndExpand"/>
                                        <Button x:Name="btn2" 
                                                Text="{Binding ItemType, Converter={StaticResource converter}, ConverterParameter=converterparameter2}" 
                                                Command="{Binding Source={x:Reference listView}, Path=BindingContext.SomeCommand}" 
                                                CommandParameter="{Binding .}"   
                                                HorizontalOptions="FillAndExpand"/>
                                    </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
</ListView>
// =============================

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>