views:

21

answers:

2

Hello all, I am hoping you can help me unravel an issue I am having with the use of a combo box.

Within my project I need to consume values from one collection and have these items display within my combobox . At this time I have built an observable collection that simply contains different involvement types

public ObservableCollection myinvolvement = new ObservableCollection();

    private void fillList() /
    {
        involvementDataModel i1 = new involvementDataModel();
        i1.involvements = "Witness";

        involvementDataModel i2 = new involvementDataModel();
        i2.involvements = "Suspect";

        involvementDataModel i3 = new involvementDataModel();
        i3.involvements = "Victim";

        involvementDataModel i4 = new involvementDataModel();
        i4.involvements = "Other";

        myinvolvement.Add(i1);
        myinvolvement.Add(i2);
        myinvolvement.Add(i3);
        myinvolvement.Add(i4);
    } 

I've then bound this to a combobox using: cmboInvType.ItemsSource = myinvolvement;

within my xaml I have the combo box setup as

<ComboBox x:Name="cmboInvType" ItemsSource="{Binding}" Grid.Row="2" Height="32" HorizontalAlignment="Left" Margin="27,5,0,0" VerticalAlignment="Top" Width="204" SelectionChanged="cmboInvType_SelectionChanged">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock x:Name="cmbvalue" TextAlignment="Center" Text="{Binding involvements}"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

This seems to work fine as I can see each unique value in the list however when I select a value from the combo box I need to pass this value to a variable that can then be sent back to the main application. I'm good to go on the passing of data part however I am stuck on how to capture the current selected value.

Whenever I select a value from the combo box I get a return of the data path ( WinODS_ClientApp.Data.involvementDataModel ) and not the selected value?

How do I go about converting this in a string to the correct string format?

So far I've tried:

to place on a SelectionChangedEventArgs
cmboInvType.SelectedItem.ToString()

however this still seems to return the path vs. the value.

I remember reading about this somewhere and I have addressed this in the past with data templates for UI presentation but not in an instance where I need to take the selected value and pass it to another variable.

Thanks for any insight!

A: 

ComboBox.SelectedItem returns an instance of involvementDataModel because that is the item that is currently selected. Just because one property is used to get the text of the item when render is not relevant to what is selected.

If you want to get the text of the selected item, I'd just get the selected item, cast it to involvementDataModel, and then get the value of the property desired.

Andy
+1  A: 

Please check

SelectedValuePath

You can configure property name as path and in SelectedValue you will get property value of the object.

Akash Kava
Thanks that worked like a charm I updated the xaml to reflect :<ComboBox x:Name="cmboInvType" ItemsSource="{Binding}" DisplayMemberPath="involvements" SelectedValuePath="involvements" Grid.Row="2" Height="32" HorizontalAlignment="Left" Margin="27,5,0,0" VerticalAlignment="Top" Width="204" />and set the variable to call:public string involvement;involvement = cmboInvType.SelectedValue.ToString();
randyc