Can workspaces be used when binding to a combo box? Strange behaviour...

Feb 15, 2011 at 2:10 PM
Edited Feb 15, 2011 at 11:34 PM

I'm having a very strange issue occuring. I am using the Workspace method in the screen & conductors documentation.

The initial screen consists of a grid, when the user click the grid item I change the "State" to the "Details" view. The grid fades out and is replaced by my details screen. The grid hosting the Details is bound to SelectedItem on the view model. Everything shows up fine except for any case where I am using a combobox.

I am populating my combobox by binding the ItemsSource to a IEnumerable<EnumItem> property on the ViewModel. EnumItem has an "Index" and "Value". The combobox is populated correctly with the correct items, but the correct SelectedItem does not get set. Here is my combobox:

 

<ComboBox Grid.Column="1" Grid.Row="2" ItemsSource="{Binding ElementName=LayoutRoot, Path=DataContext.JobTitles, Mode=OneWay}" SelectedValue="{Binding Path=Title, Mode=TwoWay, Converter={StaticResource Nullable}}"  SelectedValuePath="Index" DisplayMemberPath="Value"><ComboBox>

 

Playing around with it I am getting very strange behaviour. For instance I click to view details on an item. Combobox is blank, I set it to a value. Hit my "back" button (which goes back to the grid), select the same item, the combobox is set. I go back, click a different item and the combox is blank. Click on items near the original item, but not the same and the combo sometimes fills in the value. Other times the combobox seems to get completely stuck where I'll have it set to a value and no matter which item in the grid I click the combo looks like its still set to the exact same value. I have about 4 combos on this particular screen they all are doing this and I am certain the binding is correct. I've been hitting my head against a wall on this so any help would be very much appreciated!!!!

Other notes: If I try binding to SelectedIndex instead Caliburn complains "Specified argument was out of the range of valid values.Parameter name: SelectedIndex" on Line 13 of  SetTargetCore(DependencyPropertyChangedEventArgs e, DependencyObject d, bool setContext)

Feb 15, 2011 at 2:11 PM

BTW that converter Nullable was just something I was trying, I get the same behaviour with or without it.

Feb 16, 2011 at 1:28 AM
Edited Feb 16, 2011 at 1:30 AM

I am going crazy with this! I am using this converter and and when i first click on a record I change "State" to "Detail" and Refreshbindings. The converter fires passing an Int. When I select an item from the drop down ConvertBack shows the correct value coming through.

Then I change "State" to null so the base grid shows. Pick a new item, After a couple times of this the converter just stops firing??? 

public class NullableConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            if (value == null)
                return -1;

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            if (value is int?) {
                if ((value as int?).HasValue) {
                    int i = (value as int?).Value;
                    if (i == -1)
                        return null;
                }
            }

            return value;
        }
    }
Coordinator
Feb 16, 2011 at 1:47 AM

Can you reproduce this in a small, simple solution and email it to me? robertheisenberg at hotmail dot com