Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Bind Multiple Columns in Combobox in Silverlight

Posted by Viral Sarvaiya on October 15, 2011

Silverlight has simple combobox like below.

<Combobox x:Name="combo1"  Width="100" Height="30" />

we use ItemSource property for binding data to combobox.
we can set DisplayMemberPath property as like

<Combobox x:Name="combo1"  Width="100" Height="30"  DisplayMemberPath ="Name"  />

for the multiple property we can edit compbobox itemTemplate.
for that you have to remove DisplayMemberPath property from Combobox tag and use the below tag for binding multiple properties to Combobox.

<ComboBox x:Name="cboTest">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name, Mode=OneWay}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Surname, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

if you want to select an item and want Id of selected item then you can use SelectedValue or Selecteditem Property as like below

<ComboBox x:Name="cboTest" ItemSource="{Binding MyList, Mode=TwoWay}" SelectedValue={Binding Path=Name, Mode="TwoWay"}>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name, Mode=OneWay}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Surname, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

hope this will help you.

Advertisements

2 Responses to “Bind Multiple Columns in Combobox in Silverlight”

  1. Gene said

    This is helpful but I would like to make the values in the second column line up. How can I do this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: