1

I have a view model & a System Windows Form with a couple of list boxes

I have bound data to the contents of the list box, as below

When user interacts with box one, it updates the view model to change contents of box two

When open and close form, all is well - boxes show the right contents

I have implemented OnPropertyChanged to confirm that the view model also updates correctly

However, I have as yet failed to make the second box refresh its contents in response to OnPropertyChanged, until I close and open the form

I've tried calling form.refresh() as well as the checklistbox.refresh() after notification that the the updating function has completed

One thing I did notice was that changing the contents of the object the view is bound to didn't fire OnPropertyChanged, which I thought was odd. It only changes when the object is initialised? Am I doing something wrong there?

    protected override void OnBindingContextChanged(EventArgs e)
    {


        base.OnBindingContextChanged(e);

        Globals.ThisAddIn.TFSTeamsViewModel.PropertyChanged += OnPropertyChanged;


    }

    void LoadProjectsListBox()

    {
        try
        {
            Globals.ThisAddIn.TFSConfigViewModel.LoadListOfProjectsIntoFormControl();
            projects_list_box.DataSource = Globals.ThisAddIn.TFSConfigViewModel.ListOfProjectsFromVM.value;
            projects_list_box.DisplayMember = "name";


            Globals.ThisAddIn.TFSTeamsViewModel.LoadTeamsForProjectsChosen();
            teams_checked_list_box.DataSource = Globals.ThisAddIn.TFSTeamsViewModel.ListOfTeamsFromVM.value;
            teams_checked_list_box.DisplayMember = "name";



            SelectCheckBoxForSavedSelection();



        }

        catch (Exception ex)

        {
            System.Diagnostics.Debug.WriteLine("Debug: TFS_Config_UI_Control: LoadProjectsListBox: Exception: " + ex.Message);

        }

    }
Journeyman1234
  • 547
  • 4
  • 18
  • 1
    You need to implement `IBindingList` for data sources or use a `BindingList` or use a `BindingSource` to get change notifications. Read the answer here: [Databinding to List - See changes of data source in ListBox, ComboBox](https://stackoverflow.com/a/33625054/3110834) – Reza Aghaei Jan 28 '21 at 16:21
  • Yep, I just came back here to post that I had kept reading and I didn't realise BindingList existed (my experience on Xamarin Forms indicated List works fine with BindingContext...) Problem sorted, thank you – Journeyman1234 Jan 28 '21 at 16:50

1 Answers1

0

As above, answer was to use BindingList

Journeyman1234
  • 547
  • 4
  • 18
  • You can upvote the linked question and answer to make them more popular. You may also want to enhance your answer here with details and example or as an option you can close the post as duplicate. – Reza Aghaei Jan 28 '21 at 16:54