1

I am new to WPF, and am really frustrated by this. I have a combobox that is bound to a DataTable. The DataTable is Filled with a Stored Procedure that returns 4 items: ID, Name, Date, Contact. I want the combobox to work by filling the dropdown with the Name, and associating the ID, so that when the user selects an item I can fill a datagrid with another stored procedure that needs the ID as a parameter in a where clause.

Here is the XAML for the combobox:

<ComboBox x:Name="cbTransmittals" HorizontalAlignment="Left" Margin="0,250,0,0"    VerticalAlignment="Top" Width="500" 
          SelectedValue ="{Binding TransID, Mode=TwoWay}"
          SelectedValuePath ="TransID" 
          DisplayMemberPath="TransName"
          ItemsSource="{Binding Source={StaticResource transmittalsViewSource}}"
          SelectionChanged="cbTransmittals_SelectionChanged"/>

In the Window_Loaded method VS set up the data binding for ViewSource for me. I have put the line that sets the SelectedIndex as a test of my SelectionChanged routine, and to see if it works as expected. At times the selected item is set accordingly, but now it isn't. I am trying to get the text or the corresponding ID to no avail. Mostly when trying to get the SelectedValue I get an exception thrown because of mismatching types (casting to int does not work, and SelectedValuePath gives me a string that is from the XAML paramter above . . . not helpful).

Here is the initialization code from Window_Loaded:

        DocControlMain.dsDocControlTableAdapters.TransmittalsTableAdapter dsDocControlTransmittalsTableAdapter = 
            new DocControlMain.dsDocControlTableAdapters.TransmittalsTableAdapter();
        dsDocControlTransmittalsTableAdapter.Fill(dsDocControl.Transmittals);
        System.Windows.Data.CollectionViewSource transmittalsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("transmittalsViewSource")));
        transmittalsViewSource.View.MoveCurrentToFirst();
        cbTransmittals.SelectedIndex = 5;

How can I use cbTransmittals.SelectedItem, SelectedValue, or something else to retrieve the ID? I have a feeling it is not being bound properly, as I let VS generate the code by dragging the DataSource Element onto the ComboBox in the designer page. I have been googling all day, and still have no working code, yet I can get this functionality to work in a Windows Form app quite quickly.

Thanks for any help, Paul

Paul Gibson
  • 622
  • 1
  • 9
  • 23

2 Answers2

0

In other words you need to get the Selected Item from the combobox. You could simply create an object of ComboBoxItem and get the content or the value from it.

Could be helpful:

Get selected value from combo box in c# wpf

Get wpf combobox selected value

Community
  • 1
  • 1
Kulasangar
  • 9,046
  • 5
  • 51
  • 82
  • I've read this post, and it may work but I'm trying to bind the data so that it displays the name (text string) but associates the UID that is in the database (int) somehow. For now I just use the name (from the text property) to return the UID from a stored procedure which works but seems like extra steps. The old days of MFC had a double that one could use as a pointer or integer value very easily but that seems to be gone. – Paul Gibson Oct 11 '14 at 01:41
  • Also, the viewSource binding happens quite invisibly, and it's hard to figure out how to programatically duplicate that. Almost no documentation that I've found so far . . . but its pretty greek to me so maybe I'm seeing something that is the answer but it is obfuscated. – Paul Gibson Oct 11 '14 at 01:44
  • Ok, now I have tried a couple more things. Using the Text property gives me what was in the combobox before the selection changes, so that does not work. When I use the method in the links: ComboboxItem cbi = (Comboboxitem)cb.SelectedItem; I get an unhandled System.InvalidCastException, Unable to cast object of type System.Data.DataRowView to type System.Windows.Controls.ComboBoxItem. So I think somehow the VS IDE has mapped the combobox to a DataRowView resource and I cannot find any examples of using a combobox this way. – Paul Gibson Oct 13 '14 at 15:17
0

Ok, I've finally got it all working. I was misusing the XAML, and by butchering it the documented correct methods to retrieve the values did not work. For anyone having similar problems, in the XAML for combo or list box, the "DisplayMemberPath" should be set to the column name that has what you want to display in the list, and "SelectedValuePath" should be set to the column name that has the values you want associated. The ComboBox.SelectedValue will return the type that is in the SelectedValuePath column . . . that is, when you set that to be a column in your table of type int, the SelectedValue will be an int. Likewise if it is a string. In my case I had not set it, and so the return was the generic datatype of the combobox binding (DataRowView) as it is bound to a DataTable.

In my code above my mistake was a brain dead one: I set the selectedvaluepath to "TransID" which is a foreign key in another table, should have been set to "UID".

Paul Gibson
  • 622
  • 1
  • 9
  • 23