M-V-VM WPF: Way to maintain Databinding while passing parent object of Databound object into IValueConverter?

StackOverflow https://stackoverflow.com/questions/1949782

Question

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 ItemsSource="{Binding Path=OutcomeList}" SelectedItem="{Binding SelectedExpectedOutcome, Mode=TwoWay}" x:Name="PART_cbExpectedOutcome" Grid.Column="1" >
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <Label Content="{Binding Path=ExpectedOutcomeData, Converter={StaticResource OutcomeDataToStringConverter}, ConverterParameter=Expected }" />  
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
<ComboBox ItemsSource="{Binding Path=OutcomeList}" SelectedItem="{Binding SelectedActualOutcome, Mode=TwoWay}" x:Name="PART_cbActualOutcome" Grid.Column="2" >
    <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.

Was it helpful?

Solution 2

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>

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top