Binding Application Data to the UI : Data Binding « Data « Silverlight

Home
Silverlight
1.Animations
2.Communication
3.Containers
4.Data
5.Events
6.Graphics
7.Shapes
8.UI Controls
Silverlight » Data » Data BindingScreenshots 
Binding Application Data to the UI
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);
    }
  }
}

   
    
  
Related examples in the same category
1.Receiving Change Notifications for Bound Data
2.Validating Input for Bounded Data
3.DataBinding and INotifyPropertyChanged
4.Implementing Data Binding in Silverlight ApplicationsImplementing Data Binding in Silverlight Applications
5.Bind to a collection
6.Add a value converter to a binding using XAML
7.Without Binding
8.Text Data Binding
9.Bind to an Existing Object InstanceBind to an Existing Object Instance
10.Binding FontFamily / FontSize value for current ControlBinding FontFamily / FontSize value for current Control
11.Binding an Emoticon object to a GridBinding an Emoticon object  to a Grid
12.Binding a collection of Emoticon objects to a ListBox through the ItemsSource propertyBinding a collection of Emoticon objects to a ListBox through the ItemsSource property
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.