0

I have created a page that passes a value to a new page that will allow users to update the data. When the users selects the record to be updated the edit form opens but the data is not visible. If the value is changed and the edit button clicked it will update the value, but it is never visible. How can I show the data that is to be edited?

View Model

namespace QiApp.ViewModels
{
    public class EditTodayCasesViewModel
    {
        private SxCaseDataService _sxCaseDataService = new SxCaseDataService();

        public SxCase SelectedSxCase { get; set; }

        public ICommand EditSxCaseCommand => new Command(async () =>
            {
               await _sxCaseDataService.PutSxCase(SelectedSxCase.SxCaseId, SelectedSxCase);
            });
    }
}

Edit Page 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"
             xmlns:viewModels="clr-namespace:QiApp.ViewModels;assembly=QiApp.UWP"
             x:Class="QiApp.Views.EditTodayCasePage">
    <ContentPage.BindingContext>
        <viewModels:EditTodayCasesViewModel/>
    </ContentPage.BindingContext>

    <StackLayout>
        <Label Text="Surgery Case"/>
        <Label Text="{Binding SelectedSxCase.SxCaseId}"/>
        <Entry Text="{Binding SelectedSxCase.Record}"/>
        <Switch IsToggled="{Binding SelectedSxCase.Complete}"/>
        <Button Text="Edit Surgery Case"
                Command="{Binding EditSxCaseCommand}"/>
    </StackLayout>
</ContentPage>

Code behind

namespace QiApp.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class EditTodayCasePage : ContentPage
    {


        public EditTodayCasePage(SxCase sxCase)
        {
            InitializeComponent();

            var editTodayCasesViewModel = BindingContext as EditTodayCasesViewModel;

            editTodayCasesViewModel.SelectedSxCase = sxCase;


        }
    }
}
Waescher
  • 5,361
  • 3
  • 34
  • 51
Brady
  • 5
  • 1
  • 5
  • Your ViewModel must implement INotifyPropertyChanged, AND don't forget to set your bindings to mode 'TwoWay'. ie: ... – KFactory May 04 '18 at 07:37

1 Answers1

0

Everything is alright except that your view gets bound to a view model which stays silent if properties are changed. Your view cannot get any information on when it should update itself and hence the UI as soon as the property SelectedSxCase gets changed.

Thankfully this can be done very easily by simply implementing the common interface INotifyPropertyChanged and extending your bound properties with a code line raising the event the interface provides.

Basically it goes like this ...

private SxCase _case;
public SxCase SelectedSxCase 
{ 
    get => _case; 
    set
    {
        _case = value;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedSxCase)));
    }
}

... but there are several implementations to do that more elegant like using the CallerMemberName or even weaving the getter and setter automatically with Fody.

Waescher
  • 5,361
  • 3
  • 34
  • 51
  • I have added INotifyConfigurationPropertyChanged to the ViewModel and added mode=twoway to the Binding in the XAML form. Now I'm getting the exception System.NullReferenceException: Object reference not set to an instance of an object. – Brady May 04 '18 at 17:13