Binding Problem with Combobox and WCF Ria Services

Apr 25, 2011 at 10:21 AM
Edited Apr 25, 2011 at 11:20 AM

Hi

I have some big binding problems with comboxes. I have spent hours to find a solution but no success. I really hope someone can help me.

I have some problem with binding SelectedValue properties of a Combobox in a screen which is conducted by a Conductor<T>.Collection.OneActive.

I have the following situation:

I have a conductor which is conducting two screens, OverviewScreen and DetailScreen. OverviewScreen is just a readonly screen which allows to select a specific customer and switch to the DetailScreen. DetailScreen is also a Conductor<T>.Collection.OneActive and conducts several “SubDetailScreens”. SubDetailScreen Nr. 2 contains a the following Combobox:

Here is my combo: 

 

 

<ComboBox
	Name="Colors"
	SelectedValue="{Binding Path=SelectedColorID,Mode=TwoWay}"
    SelectedValuePath="ColorID"
    DisplayMemberPath="Name"
    Width="160"
    Grid.Column="1"
    Grid.Row="2"
    HorizontalAlignment="Left"
    Margin="4"/>

 
Here is the collection in my ViewModel:
private BindableCollection<Color> _colors;
public BindableCollection<Color> Colors
{
   get { return _ colors; }
   set
   {
      if (_colors!= value)
      {
          _ colors = value;
          NotifyOfPropertyChange(() => Colors);
       }
    }
}

Here is the bound property in the ViewModel which holds the id of the selected item.

 

private int? _selectedColorID;
public int? SelectedColorID
{
    get { return _selectedColorID; }
    set
    {
         if (_selectedColorID != value)
         {
              _ selectedColorID= value;
              NotifyOfPropertyChange(() => SelectedColorID);
           }
      }
}

I am binding to the car which is loaded in the context (shared by MEF) when I activate the screen.

protected override void OnActivate()
{
       base.OnActivate();
       if (_open)
       {
                Coroutine.BeginExecute(Load());
                _open = false;
        }
 }

public IEnumerator<IResult> Load()

{
     yield return new LoadData<Color>(CmcPlusCtx, Ctx.GetColorsQuery(), LoadBehavior.RefreshCurrent, BindCollections);
     Bind();
}

public void BindCollections(LoadOperation loadOp)
{
    var allColors = loadOp.Entities.Cast<Color>();
    Colors.AddRange(allColors);
}

public void Bind()

    CurrentCar = Ctx.Cars.FirstOrDefault();
    ColorID = CurrentCar.colorID;
 }

The problem occures when a execute the following actions:

1.       Step: Select a car in the OverviewScreen

2.       Step: Switch to the DetailScreen which is showing SubDetailScreen Nr. 1 by default.

3.       Step: Switch to the DetailScreen Nr. 2.

4.       Step: Close the whole DetailScreen (OverviewScreen is visible again).

5.       Doing Step 1 -3 again.

Now the Combobox doesn’t show the selected color of the loaded Car. Debug results are showing, that “GetBindingExpression(ComboBox.SelectedValueProperty)“ of the combobox is null.

Note: The problem doesn’t occure when I execute steps 1 – 3 for the first time (after starting the application and loading a car in the DetailScreen). When I do this for the first time, the Comboboxes are showing the correct selected color.

I really hope someone can help me.
Many thanks.
Coordinator
Apr 25, 2011 at 1:55 PM

The conventions for combo might be conflicting. Try removing the Name and adding the ItemsSource binding explicitly. See if that fixes it. Please let me know.

Apr 25, 2011 at 5:28 PM

Unfortunately, no effect.

Apr 26, 2011 at 7:26 AM

Additional information: The problem only occurs in combination with selected value properties of Combobox and AutocompleteBox. A Textbox works perfectly.

Apr 26, 2011 at 7:31 AM

May I ask you why you are binding SelectedValue instead of SelectedItem?

Apr 26, 2011 at 8:33 AM

I am working with Entities.
I consider it as good practice to load the whole items only with the collection.
The selection of the item can then be done by the colorId of the loaded "Car" over "SelectedValue".
This way a can reduce redundant data loaded to the client.

How is your way with WCF Ria Services in combination with Comboboxes? Maybe there are better ways than mine.

Apr 26, 2011 at 11:02 AM

I think there is also a good way in combination with SelectedItem. So I am going to try it out.

Thanks

Apr 26, 2011 at 12:55 PM

As far as I know, the best practice with ComboBoxes is using SelectedItem to implement the selection logic, and using SelectedValue just to display a content.

Honestly, I never use SelectedValue, since I prefer to specify a proper ItemTemplate (it makes localization simpler).

Apr 27, 2011 at 4:26 PM
Edited Apr 27, 2011 at 4:28 PM

Thanks a lot. That was the golden tip. SelectedItem works perfectly. No more binding problems.

Naming convention for the collection binding works too.

I searched in the internet for a while. A lot of developers have problems with SelectedValue. SL Combobox seems to be buggy. (Or I just maltreated it. :-))