Forum Xamarin Xamarin.Forms

Firing set event in the VM when a listview entry is updated in the view

reggiebreggieb Member ✭✭
edited August 10 in Xamarin.Forms

So, I have been having trouble getting something to work. What I am basically doing is creating a listview based on data pulled from a webservice. The listview will be a list of populated entries and editors. Upon users making changes to them, I'll be sending an update request back to the web service. I am trying to make the updates from the MVVM, since that's where the object is pulled, and where the observable collection is bound from, but it's not working. I have tried several things that don't work, but here is the state of where I am (note I'm going to strip these down to a simplified relevant part of the view and view model, if there is something else I'm missing that you'd need to see, let me know).

View

<ListView ItemsSource="{Binding HeroicLineItemsGrouped, Mode=TwoWay}"
            GroupDisplayBinding="{Binding Key}"
            IsGroupingEnabled="True"
            HasUnevenRows="true"
            VerticalOptions="FillAndExpand"
            x:Name="projectListView">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid Padding="10">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Entry Grid.Column="0"
                        Grid.Row="0"
                        Grid.ColumnSpan="2"
                        Text="{Binding Name, Mode=TwoWay}"
                        ClassId="{Binding ID}"
                        FontAttributes="Bold" />                                 
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

View Model

private ObservableCollection<Grouping<string, HeroicBudgetLineItem>> _heroicLineItemsGrouped;
public ObservableCollection<Grouping<string, HeroicBudgetLineItem>> HeroicLineItemsGrouped
{
    get => _heroicLineItemsGrouped;
    set
    {
        _heroicLineItemsGrouped = value;
        NotifyPropertyChanged("TaskList");
    }
}

public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
    System.Diagnostics.Debug.WriteLine("Notify Property Changed");
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

Basically, I am trying to figure out how to get the set event to fire when a user makes a change to the bound Entry.

Posts

  • reggiebreggieb Member ✭✭

    Alright, I think the best way to handle this from an MVVM perspective is to have a textchanged event on the Entry, and then to call a method in the VM from that event. I am new to the MVVM approach, but I think that makes the most sense. The event handler really belongs in the code behind, but then I reach out to the VM to update the model and call out to the rest service.

    I think I've got it.

Sign In or Register to comment.