views:

247

answers:

2

I'm using model-view-viewmodel I currently have a class with 3 pieces of data: 2 integers and an enumeration.

Its constructor looks like this:

//C#
public Outcome(OutcomeEnum outcomeEnum, Int32 acutalOutcomeData, Int32 expectedOutcomeData)
{
  m_outcomeEnum = outcomeEnum;
  m_actualData = acutalOutcomeData;
  m_expectedData = expectedOutcomeData;
}

I have 2 ComboBoxes next to each other that I have bound to one List of outcome objects (List<Outcome>) which I use to the "actual" and "expected" integer values.

This section of code looks like this: (The SelectedItem and ItemsSource are dependency properties in the viewmodel )

    <ComboBox 
        x:Name="PART_cbExpectedOutcome" 
        Grid.Column="1"
        ItemsSource="{Binding Path=OutcomeList}"
        SelectedItem="{Binding SelectedExpectedOutcome, Mode=TwoWay}"
        >

        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Label Content="{Binding Path=ExpectedOutcomeData, Converter={StaticResource OutcomeDataToStringConverter}, ConverterParameter=Expected }" />  
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>

    </ComboBox>

    <ComboBox 
        x:Name="PART_cbActualOutcome" 
        Grid.Column="2"
        ItemsSource="{Binding Path=OutcomeList}"
        SelectedItem="{Binding SelectedActualOutcome, Mode=TwoWay}"
        >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Label Content="{Binding Path=ActualOutcomeData, Converter={StaticResource OutcomeDataToStringConverter}, ConverterParameter=Actual}" />

                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

My problem is: I'd like to pass a reference to the Outcome object into the IValueConverter that I'm using to convert between, but this appears to not be possible using the IConvertParameter but rather I would be forced to use Multibinding per the msdn post here.

I'd like to simplify my approach as creating a multibinding for something relatively as simple as this seems like overkill.

I'd like to do only one thing with the Outcome object that I seek to pass into IValueConverter, which is to determine the enumeration type of OutcomeEnum so I can provide the proper formatting of the expected or actual data value.

Is there any simpler way for me to be able to pass the Outcome object into the IValueConverter OutcomeDataToStringConverter while maintaining a two-way binding with this list of Outcome objects? I'm open to suggestions.

A: 

Why don't you bind to the entire Outcome object instead of its ActualOutcomeData or ExpectedOutcomeData properties? That way the converter will receive the entire Outcome object, and based on it and on the converter parameter return the proper value.

<ComboBox.ItemTemplate>
    <DataTemplate>
        <StackPanel>
            <Label Content="{Binding Converter={StaticResource OutcomeToStringConverter}, ConverterParameter=Expected}"/>
        </StackPanel>
    </DataTemplate>
</ComboBox.ItemTemplate>
Aviad P.
That is a good idea, the first thing I tried. The issue is that if I bind the Outcome object on both comboboxes, if I have only one list of Outcome objects *both* comboboxes will be synchronized to the same object. In practice this means when I change one combobox it can change the value of the other combobox, e.g. you have 3 items in the list with actual/expected equal to 1/1, 2/2 and 3/3. If you change combobox 1 to 2, BOTH show 2/2, when i really want 2/1.
CrimsonX
I believe you should achieve separation in the selection of the combo boxes by setting IsSynchronizedWithCurrentItem to false... Please let me know if that's not the case.
Aviad P.
A: 

For right now, I ended up changing the Outcome class so it had only one member - outcomeData

Then I created two lists of Outcome objects which were bound to the combobox's ItemsSource - ActualOutcomeList and ExpectedOutcomeList.

I can then bind the Outcome object itself to the Combobox, but by having two lists I avoid the duplicate selection issue I describe in the comment on Avaid's post.

In the end the code looks like this:

    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Label Content="{Binding Converter={StaticResource OutcomeToStringConverter}}" />  
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>

</ComboBox>

<ComboBox 
    x:Name="PART_cbActualOutcome" 
    Grid.Column="2"
    ItemsSource="{Binding Path=ActualOutcomeList}"
    SelectedItem="{Binding SelectedActualOutcome, Mode=TwoWay}"
    IsEnabled="{Binding Path=IsOutcomeEnabled}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Label Content="{Binding Converter={StaticResource OutcomeToStringConverter}}" />

            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
CrimsonX