none
Hierarchical Child Combobox has value cleared out when Datacontext is refreshed. RRS feed

  • Question

  • We have had the challenge of refreshing our data and found blogs about setting the Datacontext = null and then re-assigning the objectdataprovider. This seems to work well for us until it comes to child hierarchy comboboxes. When the Datacontext is set to null and then re-assigned, the value of the child combo gets cleared out and doesn't rebind. The parent value is fine. Any Ideas? Need help with this one!

     

    addEditSource.ObjectInstance gets reloaded with refresh data from the database.

     

    Code Block

    //Data refresh

    this.addEditGrid.DataContext = null;

    this.addEditGrid.DataContext = addEditSource.ObjectInstance;

     

     

    //Combobox binding for hierarchy child combo.

    Binding bindingVal = new Binding();

    bindingVal.Source = PARENTCOMBOBOX;

    PropertyPath propertyPathVal = new PropertyPath(ComboBox.SelectedItemProperty);

    bindingVal.Path = propertyPathVal;

    cb.SetBinding(ItemsSourceProperty, bindingVal);

    cb.IsSynchronizedWithCurrentItem = true;

    cb.DisplayMemberPath = DISPLAYCOLUMNNAME;

     

    string bindingPath = string.Empty;

    bindingPath = "Tables[" + tableName + "].Rows[0]{0}[" + columnName + "]";

    bindingPath = string.Format(bindingPath, string.Empty);

     

    Binding bindingValue = new Binding();

    bindingValue.NotifyOnSourceUpdated = true;

    bindingValue.NotifyOnTargetUpdated = true;

    bindingValue.Mode = BindingMode.TwoWay;

    PropertyPath propertyPath = new PropertyPath(bindingPath);

    bindingValue.Path = propertyPath;

    cb.SetBinding(ComboBox.TextProperty, bindingValue);

     

     

    Wednesday, October 31, 2007 7:47 PM

All replies

  •    Instead of resetting the DataContext, have you ever tried to reset the binding at the topest level of the hierarchy?

     

       I guess you might use TreeView for the hierarchical data binding, then you can refresh binding using the following code:

     

    Code Block

    public void RefreshBinding(TreeView treeView, Binding binding)

    {

        BindingOperations.ClearBinding(treeView, TreeView.ItemsSourceProperty);

        treeView.SetBinding(TreeView.ItemsSourceProperty, binding);

    }

     

       Hope this helps
    Friday, November 2, 2007 3:37 AM
  • I don't quite understand the TreeView reference here. I have a grid that has a bunch of controls in it that needs their data refresh based on the DataContext of the Grid. What am I passing to this method RefreshBinding? I have already tried ClearBinding and re-adding the binding of the ItemSource of the dependant combobox after I have reset the DataContext. This still has unexpected results.

     

    Code Block

    //Remove itemsource binding to parent element. Deals with bug where it nulls out child value

    //after refreshing datacontext.

    RemoveHierarchyBindingFromComboBox();

     

    //Refresh the data on the form.

    ObjectDataProvider addEditSource = this.addEditGrid.FindResource("EditResultData") as ObjectDataProvider;

    this.addEditGrid.DataContext = null;

    this.addEditGrid.DataContext = addEditSource.ObjectInstance;

     

    //Re-add hierarchy combobox bindings.

    AddHierarchyBindingToComboBox();

     

    private void RemoveHierarchyBindingFromComboBox()

    {

    XmlDocument metadata = MetadataManager.Instance.LoadMetadata(this.FunctionName);

    XmlNodeList controls = metadata.GetElementsByTagName("parent_element_name");

    foreach (XmlNode control in controls)

    {

    MetrixComboBox comboControl = ((MetrixComboBox)(this.addEditGrid.FindName(control.ParentNode.SelectSingleNode("name_to_register").InnerText)));

    BindingOperations.ClearBinding(((DependencyObject)(comboControl)), MetrixComboBox.ItemsSourceProperty);

    }

    }

    private void AddHierarchyBindingToComboBox()

    {

    XmlDocument metadata = MetadataManager.Instance.LoadMetadata(this.FunctionName);

    XmlNodeList controls = metadata.GetElementsByTagName("parent_element_name");

    foreach (XmlNode control in controls)

    {

    MetrixComboBox parentComboControl = ((MetrixComboBox)(this.addEditGrid.FindName(control.ParentNode.SelectSingleNode("parent_element_name").InnerText)));

    MetrixComboBox comboControl = ((MetrixComboBox)(this.addEditGrid.FindName(control.ParentNode.SelectSingleNode("name_to_register").InnerText)));

    Binding bindingVal = new Binding();

    bindingVal.Source = parentComboControl;

    PropertyPath propertyPathVal = new PropertyPath(ComboBox.SelectedItemProperty);

    bindingVal.Path = propertyPathVal;

    comboControl.SetBinding(MetrixComboBox.ItemsSourceProperty, bindingVal);

    comboControl.IsSynchronizedWithCurrentItem = true;

    comboControl.DisplayMemberPath = control.ParentNode.SelectSingleNode("lookup_display_column").InnerText;

    }

    }

     

     

     

    Friday, November 2, 2007 7:37 PM
  • Hi Rod,

     

       Since I still cannot figure out the real cause of this problem given the code snippet you posted here, could you please send the whole test project over to me for further research?

     

       Thanks

    Tuesday, November 6, 2007 5:02 AM
  • I don't really have any test projects that I can give you. The real problem is that I have two comboboxes in a hierarchy. When I select a value in the parent the child values change appropriately. These two combos are bound to Xml and work just fine. The problem comes from the binding to the record source which is dataset held in an ObjectDataProvider. The binding on the child combo is set on the TextProperty. For what ever reason, it is not binding correctly because the value for the child combo is blank when a record is being viewed.

     

    I have another part of the app where the two comboboxes are actually bound to an XmlDataProvider for the record data and the child combo works just fine.

     

    Code Block

    Child Combobox binding for record value.

    string bindingPath = string.Empty;

    bindingPath = "Tables[" + tableName + "].Rows[0]{0}[" + columnName + "]";

    bindingPath = string.Format(bindingPath, string.Empty);

    Binding bindingValue = new Binding();

    bindingValue.NotifyOnSourceUpdated = true;

    bindingValue.NotifyOnTargetUpdated = true;

    bindingValue.Mode = BindingMode.TwoWay;

    PropertyPath propertyPath = new PropertyPath(bindingPath);

    bindingValue.Path = propertyPath;

    cb.SetBinding(ComboBox.TextProperty, bindingValue);

     

    In example A: When ObjectDataProvider is refreshed the child combo value disappears.

    ObjectDataProvider addEditSource = this.addEditGrid.FindResource("EditResultData") as ObjectDataProvider;

    this.addEditGrid.DataContext = null;

    //ObjectInstance is a dataset

    this.addEditGrid.DataContext = addEditSource.ObjectInstance;

     

    In example B: When the XmlDataProvider is refreshed the child combo value gets updated correctly.

    //Save the response to the XMLDataProvider that the controls are bound to.

    ((XmlDataProvider)(this.FindResource("TaskEditData"))).Document = objResponse;

    ((XmlDataProvider)(this.FindResource("TaskEditData"))).Refresh();

     

     

    Here is the combos xml that there are bound to for display. If you pick product_family from combo1 then you only see product_name values corresponding to combo1's value.

     

    <product_family_hierarchy_select_result result_name="">

    <product_family>

    <product_family></product_family>

    <prod_fam_and_nm></prod_fam_and_nm>

    <prod_fam_and_nm></prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>BDBFAMILY</product_family>

    <prod_fam_and_nm>

    <product_family>BDBFAMILY</product_family>

    <product_name>BDBNAME1</product_name>

    </prod_fam_and_nm>

    <prod_fam_and_nm>

    <product_family>BDBFAMILY</product_family>

    <product_name>BDBNAME2</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>BEVERAGE</product_family>

    <prod_fam_and_nm>

    <product_family>BEVERAGE</product_family>

    <product_name>DRINK</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>CLEANING SYS</product_family>

    <prod_fam_and_nm>

    <product_family>CLEANING SYS</product_family>

    <product_name>CLEANING SYS</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>COMPUTER</product_family>

    <prod_fam_and_nm>

    <product_family>COMPUTER</product_family>

    <product_name>COMPUTER</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>COPY/FAX</product_family>

    <prod_fam_and_nm>

    <product_family>COPY/FAX</product_family>

    <product_name>COPY/FAX</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>FUN</product_family>

    <prod_fam_and_nm>

    <product_family>FUN</product_family>

    <product_name>FUNNY</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>GENERIC</product_family>

    <prod_fam_and_nm>

    <product_family>GENERIC</product_family>

    <product_name>BAD DATA</product_name>

    </prod_fam_and_nm>

    <prod_fam_and_nm>

    <product_family>GENERIC</product_family>

    <product_name>CORRUPTED DATA</product_name>

    </prod_fam_and_nm>

    <prod_fam_and_nm>

    <product_family>GENERIC</product_family>

    <product_name>GENERIC</product_name>

    </prod_fam_and_nm>

    <prod_fam_and_nm>

    <product_family>GENERIC</product_family>

    <product_name>INVALID DATA</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>GFS</product_family>

    <prod_fam_and_nm>

    <product_family>GFS</product_family>

    <product_name>GFS</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>JOHNSFAMILY</product_family>

    <prod_fam_and_nm>

    <product_family>JOHNSFAMILY</product_family>

    <product_name>JOHNSPRODUCTNAME</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>KEN</product_family>

    <prod_fam_and_nm>

    <product_family>KEN</product_family>

    <product_name>KEN</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>MB SYSTEMS</product_family>

    <prod_fam_and_nm>

    <product_family>MB SYSTEMS</product_family>

    <product_name>MB SYSTEMS</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>MEDICAL</product_family>

    <prod_fam_and_nm>

    <product_family>MEDICAL</product_family>

    <product_name>MEDICAL</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>METER</product_family>

    <prod_fam_and_nm>

    <product_family>METER</product_family>

    <product_name>METER</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>MODEL01</product_family>

    <prod_fam_and_nm>

    <product_family>MODEL01</product_family>

    <product_name>MODEL</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>PM PRODUCT</product_family>

    <prod_fam_and_nm>

    <product_family>PM PRODUCT</product_family>

    <product_name>PM PRODUCT</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>QATRFAMILY</product_family>

    <prod_fam_and_nm>

    <product_family>QATRFAMILY</product_family>

    <product_name>QATRNAME1</product_name>

    </prod_fam_and_nm>

    <prod_fam_and_nm>

    <product_family>QATRFAMILY</product_family>

    <product_name>QATRNAME2</product_name>

    </prod_fam_and_nm>

    </product_family>

    <product_family>

    <product_family>TELECOMM</product_family>

    <prod_fam_and_nm>

    <product_family>TELECOMM</product_family>

    <product_name>TELECOMM</product_name>

    </prod_fam_and_nm>

    </product_family>

    </product_family_hierarchy_select_result>

     

    Tuesday, November 6, 2007 4:49 PM
  • Did you ever solve this problem? I am facing the same issue.
    Tuesday, January 25, 2011 11:43 PM