Mega Code Archive

 
Categories / Silverlight / UI Controls
 

Use ItemControl the bind data

<UserControl x:Class='SilverlightApplication3.MainPage'     xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'      xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'     xmlns:d='http://schemas.microsoft.com/expression/blend/2008'      xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'      mc:Ignorable='d'      d:DesignWidth='640'      d:DesignHeight='480'>     <Grid x:Name="LayoutRoot" Background="Beige">         <ItemsControl x:Name="_cities">             <ItemsControl.ItemTemplate>                 <DataTemplate>                     <TextBlock FontSize="14" Height="30" Text="{Binding ItemName}" ></TextBlock>                 </DataTemplate>             </ItemsControl.ItemTemplate>         </ItemsControl>     </Grid> </UserControl> //File: Page.xaml.cs using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Browser; namespace SilverlightApplication3 {     public class ItemData     {         public string ItemName { get; set; }         public double X {get;set;}         public double Y {get;set;}         public ItemData(string strName, double nX, double nY)         {             ItemName = strName;             X = nX;             Y = nY;         }     }     public partial class MainPage : UserControl     {         List<ItemData> myCities;         String strCurrentCity;         Double nCurrentX;         Double nCurrentY;         public MainPage()         {             InitializeComponent();             this.Loaded += new RoutedEventHandler(Page_Loaded);             upDateCities("uk");         }         void Page_Loaded(object sender, RoutedEventArgs e)         {             HtmlPage.RegisterScriptableObject("MySilverlightObject", this);         }           [ScriptableMember]         public void upDateCities(string strCountry)         {             myCities = getCities(strCountry);             _cities.ItemsSource = myCities;         }         private List<ItemData> getCities(string strCountry)         {             List<ItemData> ret = new List<ItemData>();                          switch (strCountry)             {                 case "france":                     {                         ret.Add(new ItemData("A", 48, 2));                         ret.Add(new ItemData("B", 43, 0));                         ret.Add(new ItemData("C", 43, 1));                         break;                     }                 case "uk":                     {                         ret.Add(new ItemData("E", 51, 0));                         ret.Add(new ItemData("F", 52, -1));                         ret.Add(new ItemData("G", 55, -3));                         break;                     }                 case "germany":                     {                         ret.Add(new ItemData("Q", 52, 13));                         ret.Add(new ItemData("W", 48, 11));                         ret.Add(new ItemData("H", 53, 9));                         break;                     }             }             return ret;         }     } }