none
Setting DataContext not updating Bound controls RRS feed

  • General discussion

  • I have a field in the page that contains the current record, initialized to null at first

     

    private EventCompanyTableAttrs group = null;

     

    The class is a simple class in which everything is exposed as a public property but implements no interfaces for notification.

     

    It is set either when clicking the New button which ends up setting it to a new instance filled with default values

     

    group = ed.NewGroup();

    PopulateGroupControls(group);

     

    Or when selecting an existing group from a listbox, which is set to an ObservableCollection full of EventCompanyTableAttrs

     

    ListBox lb = (ListBox)sender;

    if (lb.IsKeyboardFocusWithin)

    if (e.AddedItems.Count > 0)

    {

    group = (EventCompanyTableAttrs)e.AddedItems[0];

    PopulateGroupControls(theGroup);

    }

     

    This used to be where I set the controls explicitly and dckGroupName is a DockControl containing the TextBox:

     

    private void PopulateGroupControls(EventCompanyTableAttrs theGroup)

    {

    dckGroupName.DataContext = theGroup;

    }

     

    The Control binding is to a Prop

    Text="{Binding Path=EventCompanyName, Mode=OneWay}"

     

    It all works just fine, with selecting items in the listbox updating the textbox, and the New button clearing it, and then selecting on the listbox with fill it with the selected information again.

     

    That is until I enter data and save it.  Whole bunch of checks, including whether somebody has deleted the record in the database or changed its data, are done before filling the group with data from the controls and adding or updating both in the database and the ObservableCollection.  Then if it was New Group, it sets up another new group so the user can quickly add bunches of new records in a row.

     

    For some reason, setting DataContext to a new Record doesn't update the Textbox.  If editing and saving an existing group,  making new selections in the listbox which sets DataContext no longer update the Textbox.

     

    Why should it work at first and then stop working?  How can I force it to update?

     

    ADDITIONAL INFORMATION:

     

    With the following, I discovered that the Binding is broken before the save, then a little experimenting showed it is broken as soon as I start typing in the textbox.  Does it have to do with the Textbox being TwoWay by default?

     

    BindingExpression be = tbxGroupName.GetBindingExpression(TextBox.TextProperty);

    Tuesday, April 15, 2008 5:16 PM

All replies

  •  MarkR1000 wrote:

    The class is a simple class in which everything is exposed as a public property but implements no interfaces for notification


    First off, please implement the INotifyPropertyChanged interface to enable change notification to see if it helps with this problem.

    Thanks

    Thursday, April 17, 2008 4:00 AM
  • We are changing the issue type to “Comment” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by editing your initial post and changing the radio button at the top of the post editor window. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

     

    Thanks!

    Monday, April 21, 2008 9:59 AM
  •  Marco Zhou - MSFT wrote:

    We are changing the issue type to “Comment” because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question” by editing your initial post and changing the radio button at the top of the post editor window. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.

     

    Thanks!

     

    Sorry I didn't get back here, but the problems stopped when I changed everything to TwoWay - Explicit.  I got to use DataContext to load the data into the controls, but the original DataContext wasn't changed by changes in the controls so I could still use it to compare to the data in the database to detect changes having been made by other users.

    Monday, April 21, 2008 10:44 PM