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

(Prism) Binding DelegateCommand not updating in Xaml

$
0
0

I'm using Prism. I can't seem to get DelegateCommand binding working in Xaml. I create the binding after receiving the Fault object from the previous ViewModel.

The ViewModel.

    public class FaultEditPageViewModel : ViewModelBase
    {

        private FaultModel _fault;
        public FaultModel Fault
        {
            get => _fault;
            set => SetProperty(ref _fault, value);
        }

        public DelegateCommand FaultEditCommand { get; set; }

        public FaultEditPageViewModel(INavigationService navigationService) : base(navigationService)
        {
    Title = "Muokkaa kone- ja laitevikaa";

            FaultEditCommand = new DelegateCommand(FaultEdit);
        }

        public override void OnNavigatedTo(INavigationParameters parameters)
        {
            Fault = parameters["fault"] as FaultModel;

            if (Fault.WorkToBeDoneArray != null)
            {
                foreach (var WorkToBeDone in Fault.WorkToBeDoneArray)
                {
                    // DelegateCommand doesn't work?? xaml doesn't get notified of this

                    WorkToBeDone.WorkToBeDoneDeleteCommand = new DelegateCommand<WorkToBeDoneArray>(WorkToBeDoneDelete);
                }
            }

            // same here, xaml doesn't get notified of this

            Fault.WorkToBeDoneAddCommand = new DelegateCommand(WorkToBeDoneAdd);
        }

        private void WorkToBeDoneAdd()
        {
            Debug.WriteLine("WorkToBeDoneAdd");

            Fault.WorkToBeDoneArray.Add(new WorkToBeDoneArray
            {
                WorkToBeDoneDeleteCommand = new DelegateCommand<WorkToBeDoneArray>(WorkToBeDoneDelete)
            });
        }

        private void WorkToBeDoneDelete(WorkToBeDoneArray WorkToBeDone)
        {
            Debug.WriteLine("WorkToBeDoneDelete");

            Fault.WorkToBeDoneArray.Remove(WorkToBeDone);
        }
}

The Model, contains a lot of data, simplified it.

    public class WorkToBeDoneArray
    {
        [JsonProperty("description")]
        public string Description { get; set; }

        public DelegateCommand<WorkToBeDoneArray> WorkToBeDoneDeleteCommand { get; set; }
    }

    public class FaultModel
    {
        [JsonProperty("workToBeDoneArray")]
        public ObservableCollection<WorkToBeDoneArray> WorkToBeDoneArray { get; set; }

        public DelegateCommand WorkToBeDoneAddCommand { get; set; }
    }

Xaml, simplified.

    <ScrollView>
        <StackLayout Padding="20, 20, 20, 20">
            <StackLayout BindingContext="{Binding Fault}">
                <Label Text="Tehtävät työt" FontAttributes="Bold"/>
                <Button Text="Lisää työtehtävä" Command="{Binding WorkToBeDoneAddCommand}"/>
                <StackLayout BindableLayout.ItemsSource="{Binding WorkToBeDoneArray}">
                    <BindableLayout.ItemTemplate>
                        <DataTemplate>
                            <StackLayout>
                                <Label Text="Tehtävä työ" FontAttributes="Bold"/>
                                <Entry Text="{Binding Description}" Placeholder="Tehtävä työ" />
                                <Button Text="Poista työtehtävä" Command="{Binding WorkToBeDoneDeleteCommand}" CommandParameter="{Binding}"/>
                            </StackLayout>
                        </DataTemplate>
                    </BindableLayout.ItemTemplate>
                </StackLayout>
            </StackLayout>
            <Button Text="Muokkaa" Command="{Binding FaultEditCommand}" />
        </StackLayout>
    </ScrollView>

Unrelated to this issue but does anyone know why I cant register nor login using Firefox, it just hangs/loads forever.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



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