megaman (5) [Avatar] Offline
#1
Hi Guys
i want to display in my wpf-combobox a list of items, but i dont wanna display the ids of my items but only the number of each item. for instance i have a query that return 3 object and i wanna display:
1 -> first object
2 -> second object
3 -> thirst object.
Thanks in Advance smilie
jwooley (123) [Avatar] Offline
#2
Re: number of items
I'm far from an expert in WPF, but the following appears to work:

Add the combo box to your form. Set the DisplayMemberPath and SelectedValuePath to the properties you want as your text and id respectively. Then set a binding for the SelectedValue to the value from your underlying object that you wish to display. The following displays the publishers from our publisher table in the drop down list and binds it to the first book found in the books table from the sample application in our book.

<Window x:Class="Window1" >
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlnssmilie="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" Name="Window1">
<StackPanel Name="RootLayout">
<ComboBox SelectedValue="{Binding PublisherId}" DisplayMemberPath="Name" SelectedValuePath="ID" Height="23" Name="PublisherCbo" VerticalAlignment="Top" />
</StackPanel>
</Window>

Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

Dim dc As New LinqBooksDataContext()

Dim publisherBinding As New Binding() With {.Source = dc.Publishers}
Me.PublisherCbo.SetBinding(ComboBox.ItemsSourceProperty, publisherBinding)

Dim firstBook = dc.Books.First
Me.RootLayout.DataContext = firstBook

End Sub