IT tutorials
 
Mobile
 

Windows Phone 8 : Controls - Data Binding (part 1) - Simple Data Binding, Using a DataTemplate

3/4/2013 6:43:14 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Writing applications for the phone will likely involve data of some sort. Silverlight’s support for data binding helps you build your applications in a much more powerful way, but what is data binding exactly? Data binding is simply a way to take data from a source (e.g., the value of a property on an object) and show it in a XAML element. If that XAML element is a control, it also supports pushing changes back to the source of the data. While that is a pretty basic explanation, the explanation is correct. Data binding is profoundly simple, and because it is simple, it is powerful.

1. Simple Data Binding

At the most basic level, a binding is a connector to pull data from a data source and put it in an element’s property. As you can see in Figure 1, a binding is in the middle of the data and the element (Control).

Figure 1. Simple data binding

Image

Bindings are defined as a markup extension. For example, here is a TextBox bound to the Name property:

<TextBox Text="{Binding Name, Source={StaticResource myData}}" />

The Binding markup extension first takes a path to the property to be bound to and then a number of optional elements. As you can see in this example, the binding is pulling from a resource object called myData. When the data binding happens, it takes the source of the data binding and uses the property path (the Name in this case) to navigate to the data it needs to put in the TextBox’s Text. The path to the property must be a public property as it uses reflection to call the getter of the public property to access the data from the source.

Having to specify the source for bindings is relatively rare, though, because when the source of an object changes you would have to change it in a number of places. For example, this XAML would show a simple editor for some data:

<StackPanel>
  <TextBlock>Name</TextBlock>
  <TextBox Text="{Binding Name, Source={StaticResource myData}}" />
  <TextBlock>Phone Number</TextBlock>
  <TextBox Text="{Binding Phone, Source={StaticResource myData}}" />
  <TextBlock>BirthDate</TextBlock>
  <TextBox Text="{Binding BirthDay, Source={StaticResource myData}}" />
</StackPanel>

Imagine that if the source changed, all of the TextBoxes would need to be rebound. Instead, data binding uses a property called DataContext, which simply allows for the source of the data binding to exist along the hierarchy of the XAML. For example, if the DataContext were set at the StackPanel, all the controls that attempt data binding inside the StackPanel would get their data from the DataContext instead of needing specific sources:

<StackPanel DataContext="{StaticResource myData}">
  <TextBlock>Name</TextBlock>
  <TextBox Text="{Binding Name}" />
  <TextBlock>Phone Number</TextBlock>
  <TextBox Text="{Binding Phone}" />
  <TextBlock>BirthDate</TextBlock>
  <TextBox Text="{Binding BirthDay}" />
</StackPanel>

When the bindings pull their data they will look for a source, and when they don’t have one they’ll search for the first non-null data source in the hierarchy. In this case they will find it at the StackPanel level and use that and the source for the data binding. The search for a DataContext will continue up the hierarchy until a valid DataContext is found. This walking of the XAML tree is not limited to the current XAML document. If the data binding is happening inside a control that is used on another XAML document, it will continue up through all the parents until it exhausts the entire object tree.

Data binding supports three modes, as described in Table 1.

Table 1. Data Binding Modes

Image

The pushing and pulling of changes is performed via reflection so that all binding modes work with any .NET class. There are no requirements for that class to work with data binding. The one exception to that is if you want changes to the source object itself to be reflected in the controls via data binding. For that to work, your source classes must support a simple interface called INotifyPropertyChanged. When the source data changes, it notifies the binding that the data has changes that will cause the binding to reread the data and change the data in the control, as shown in Figure 2.

Figure 2. Changes in the source

Image

2. Using a DataTemplate

List controls can show any list that supports IList or IEnumerable, but that’s only part of the story. List controls also support the ability to use DataTemplates to customize the look of individual items in the list. You can use a DataTemplate to specify the ItemTemplate to use arbitrary XAML to define what is contained in a ListBox. For example:

<ListBox ItemsSource="{StaticResource theData}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel>
        <TextBlock Text="{Binding Name}" />
        <Image Source="{Binding ImageUrl}" />
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

As this ListBox creates its individual items, it will use the DataTemplate as a factory to create the XAML that is contained inside. The DataContext for the created XAML becomes the individual item to be shown in the ListBox so that the data binding inside the DataTemplate just works.

 
Others
 
- iPhone SDK 3 : Making Connections with GameKit and Bonjour - iPhone to iPhone Gaming Via BonjourHelper
- iPhone SDK 3 : Making Connections with GameKit and Bonjour - Working Around Real-World GameKit Limitations
- Android : Getting Fancy with Lists - Interactive Rows
- Android : Getting Fancy with Lists - Better. Stronger. Faster.
- Windows Phone 7 : Designing the Game Framework (part 3) - The GameHost Class
- Windows Phone 7 : Designing the Game Framework (part 2) - The TextObject Class
- Windows Phone 7 : Designing the Game Framework (part 1) - The GameObjectBase Class, The SpriteObject Class
- Windows Phone 7 : Getting Started with XNA - Other Graphics Options
- iPad : Your Calendar - Adding New Calendar Appointments, Events (part 2)
- iPad : Your Calendar - Adding New Calendar Appointments, Events (part 1)
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us