Mega Code Archive

 
Categories / Silverlight / Data
 

Binding Application Data to the UI

<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'   xmlns:local="clr-namespace:SilverlightApplication3" >   <UserControl.Resources>     <local:Company x:Key="CLRDS_Company" />   </UserControl.Resources>   <StackPanel x:Name="LayoutRoot" DataContext="{StaticResource CLRDS_Company}">     <TextBlock x:Name="tbxCompanyName"/>     <TextBlock Text="{Binding Street}"/>     <TextBlock Text=","/>     <TextBlock Text="{Binding City}"/>     <TextBlock Text=","/>     <StackPanel Margin="0,0,0,8" Orientation="Horizontal">       <TextBlock Margin="0,0,5,0" Text="{Binding State}" />       <TextBlock Text="{Binding Zip}"/>     </StackPanel>     <ListBox x:Name="lbxEmployees" ItemsSource="{Binding Employees}">       <ListBox.ItemTemplate>         <DataTemplate>           <StackPanel>             <TextBlock Grid.Column="0" Text="{Binding FirstName}"                         Margin="0,0,5,0" />             <TextBlock Grid.Column="1" Text="{Binding LastName}"                         Margin="0,0,5,0"/>             <TextBlock Grid.Column="2" Text="{Binding PhoneNum}"/>           </StackPanel>         </DataTemplate>       </ListBox.ItemTemplate>     </ListBox>   </StackPanel>   </UserControl> //File: Page.xaml.cs using System.Windows.Controls; using System.Windows.Data; using System.Collections.Generic; namespace SilverlightApplication3 {   public class Employee   {     public string FirstName { get; set; }     public string LastName { get; set; }     public long PhoneNum { get; set; }   }   public class Company   {     public string Name { get; set; }     public string Street { get; set; }     public string City { get; set; }     public string State { get; set; }     public int ZipCode { get; set; }     public List<Employee> Employees { get; set; }     public Company()     {       this.Name = "A";       this.Street = "5l Street";       this.City = "New York";       this.State = "NY";       this.ZipCode = 10005;       this.Employees = new List<Employee>();       this.Employees.Add(           new Employee           {             FirstName = "A",             LastName = "B",             PhoneNum = 2           });       this.Employees.Add(           new Employee           {             FirstName = "C",             LastName = "D",             PhoneNum = 7           });     }   }   public partial class MainPage : UserControl   {     public MainPage()     {       InitializeComponent();       Binding CompanyNameBinding = new Binding("Name");       CompanyNameBinding.Mode = BindingMode.OneWay;       tbxCompanyName.SetBinding(TextBlock.TextProperty,CompanyNameBinding);     }   } }