Tuesday, September 08, 2009

Silverlight ComBoBox Selected Item not Setting

Here is the code I developed to Bind data to a ComboBox and set the selected Item. This code is tested and working fine. Xaml page

<UserControl x:Class="WorldOfVenkat.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="900" Height="600">

<Canvas x:Name="LayoutRoot" Background="Black">

<ComboBox Name="comBoBox" Width="400" Height="30" ></ComboBox>

</Canvas>

</UserControl>

Code behind in C#

public partial class Page : UserControl

{ public Page()

{

InitializeComponent(); BindComBoBox(); }

private void BindComBoBox()

{

List<Country> coutries = new List<Country>();

Country India = new Country(1,"India");

Country USA = new Country(2, "USA");

Country Canada = new Country(3,"Canada");

coutries.Add(India);

coutries.Add(USA);

coutries.Add(Canada);

comBoBox.ItemsSource = coutries; comBoBox.DisplayMemberPath = "Name"; comBoBox.SelectedItem = Canada;

}

public class Country

{

public int ID { get; set; }

public string Name { get; set; }

public Country(int id,string name)

{

ID = id;

Name = name;

}

} }

No comments: