Code Simplified – Viral Sarvaiya

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

Archive for October, 2011

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

Posted in .Net, ASP.NET, Silverlight | Tagged: , , , , , | 2 Comments »

 
%d bloggers like this: