-2

I have a WPF application that has a Combobox that is supposed to be bound to a boolean value. The UI should have a combobox with just two choices to choose from. Here is what I have so far. The XAML:

<DataGridTemplateColumn Header="Instance" Width="Auto">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate DataType="{x:Type dataModel:ParameterSetting}">
            <Grid Visibility="{Binding IsPlaceholder, Converter={StaticResource BoolToVisInvert}}">
                <ComboBox SelectedIndex="{Binding Instance, Mode=TwoWay, Converter={StaticResource ConvBoolToInstance}}">
                    <ComboBoxItem>Instance</ComboBoxItem>
                    <ComboBoxItem>Type</ComboBoxItem>
                </ComboBox>
            </Grid>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

The referenced converter:

[ValueConversion(typeof(bool), typeof(int))]
public class ConvBoolToInstance : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) return 0;
        if (!(value is bool e))
            throw new ArgumentException(@"Value was not a boolean and could not be converted", nameof(value));

        return e ? 0 : 1;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (DesignerProperties.GetIsInDesignMode(new DependencyObject())) return true;
        if (value == null || !(value is int i))
            throw new ArgumentException(@"Value was not an integer and could not be converted", nameof(value));

        return i == 0;
    }
}

And the property that it's trying to bind to:

/// <summary>
/// If the parameter should be added as instance
/// </summary>
public bool Instance
{
    get => _instance;
    set
    {
        if (_instance == value) return;
        _instance = value;
        OnPropertyChanged(nameof(Instance));

    }
}

When I debug this I get the correct choices in the combobox. I can change the default of the field and the combobox displays the correct item, plus I can set a breakpoint on the getter for the property and it's hit so I know that is correctly bound and going to the property I want it to. However, when I change the combobox value it changes the UI but doesn't push back to the property value on the object. I have set break points in both the property setter and the converter and neither are hit. From what I can tell it's just not pushing it back which is what it should do with a two way binding...

What am I missing?

EDIT

The converter is thanks to this answer but I have also tried biding to it as a SelectedValue with boolean combobox items but nothing seems to make it bind back...

EDIT

I should also mention this is inside a DataGrid. Updated question to show more of the XAML

sfaust
  • 2,089
  • 28
  • 54
  • Did you add `` to your resources section, similar to the [answer you linked](https://stackoverflow.com/questions/4335339/how-to-bind-a-boolean-to-combobox-in-wpf/4335392#4335392) to? – quaabaam Sep 15 '20 at 00:17
  • Well I used 'converters' instead of 'local' but yes I did. It should be defined. I Also tried it without a converter, just showing the direct boolean true/false and same result. – sfaust Sep 15 '20 at 01:45

1 Answers1

0

Ok finally figured this out and a bit of a face palm, but posting the answer to help anyone in my same position. The answer was to use UpdateSourceTrigger=PropertyChanged. I believe it defaults to LostFocus which is fine but in this case it was REALLY delayed. Like I selected something in the ComboBox, then clicked into another cell to edit it, then clickout out of THAT cell which apparently finally took focus because THEN it committed. Not sure why it would be so delayed like that but once I switched to PropertyChanged it worked like a charm.

Here is my final binding (after figuring this out I was also able to just use normal binding without the converter):

SelectedItem="{Binding Instance, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
sfaust
  • 2,089
  • 28
  • 54