0

I have a combo box in a datagrid located in an activity. Based on combobox selection I populate another grid with controls programatically. User enters some data in these controls and then saves it. The object that the combo box is bound has many properties of which two are used in selected value path and display member path. The data is bound using two way binding for combo box. When the saved activity that has been placed on a workflow is reopened the data is reloaded correctly and the correct object is value is set in the combo box. But on UI rendering only the values that are attached with the combo box remain intact (i.e those in selected value path and display member path) the rest are reset.

Any idea why this might be happening?

P.S: Setting the binding to OneTime solves the problem of retrieval but any changes that are made on the UI after loading are not reflected back.

Code-Behind:

public ObservableCollection<MyRule> AllRules {get;set;}
public MyRule myRule{get;set;}

In datagrid Loaded Event I populate the AllRules as:

AllBusinessRules.Add(new MyRule () { RuleId = item.Id, RuleName = item.Name});

where item.Id and item.Name are obtained from the database via service call.

In the same event if I also load any previously saved rules as:

myRule=SelectedRule; 

where SelectedRule has RuleId, RuleName, Inputs and Outputs as well.

Code:

     <ComboBox  
        ItemsSource="{Binding Path=AllRules}"
       SelectedItem="{Binding Path=myRule,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
        SelectedValuePath="RuleId" 
        DisplayMemberPath="RuleName">
        <DataTemplate>
            <TextBox Text="{Binding Path=myRule.RuleName}"/>
        </DataTemplate>

    </ComboBox>

Class:

  public class MyRule{
    public int RuleId{get;set;}
    public string RuleName{get;set;}
    public List<string> Inputs{get;set;}   //properties that are reset when the UI renders
    public List<string> Outputs{get;set;}  //properties that are reset when the UI renders
    
    }

The Inputs and Outputs properties are obtained from the programatically generated controls via reflection and added to the object populated by combobox and saved.

I have studied about this problem here but the solution does not solve my problem. Any help would be great.

Community
  • 1
  • 1
Syed Osama Maruf
  • 1,895
  • 2
  • 20
  • 37

2 Answers2

1

SelectedValuePath, DisplayMemberPath are set wrongly. DisplayMemberPath should be "RuleName". SelectedValue, SelectedValuePath are not needed as you have set SelectedItem. SelectedItem will get the chosen item automatically because of Binding. From the myRule object you can access other properties.

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
  • Thanks AnjumSKhan. Will let you know if this worked. But even so the SelectedItem is chosen correctly its just that only the values that are bound to the combo box are retained upon rendering. – Syed Osama Maruf Jan 10 '16 at 18:05
  • Still the same issue. The thing is that the in the data grid loaded event the object has correct values but when the combo box selection changed event is called the values of input and output are reset. – Syed Osama Maruf Jan 11 '16 at 05:15
0

It took a lot of time to investigate what was wrong but now that I know its just quite simple.

As I have shown that in the datagrid's Loaded event I used to set the ItemsSource of the combo box and in the item source I have only set the properties RuleId and RuleName.

Problem: So the problem was that when I assigned the value i.e the selected value on reloading the combobox e.g. myRule=SelectedRule the other properties i.e. Inputs and Outputs are not there in the ItemsSource. That is why the selected object though correct did not have Inputs and Outputs as the SelectedItem was from ItemsSource of the Combo Box giving me the impression that the two way binding had somehow reset the values of properties not bound with the combo box.

Solution: In the end I wrapped my MyRule Object in another object like RuleInformation i.e

public class RuleInformation{
public List<string>  Inputs;
public List<string>  Outputs;
public MyRule myRule{get;set;} 

}

where MyRule is like:

public class MyRule{
public int RuleId{get;set;}
public string RuleName{get;set;}
}

So the combo box is bound to the MyRule object whereas the inputs and output properties remain untouched in the upper object.

Syed Osama Maruf
  • 1,895
  • 2
  • 20
  • 37