dcsimg
March 30, 2017
Hot Topics:

Meet DependencyObject and DependencyProperty, the .NET Data-Binding Boosters

  • December 13, 2006
  • By Chad A. Campbell
  • Send Email »
  • More Articles »

The code sample displays something slightly different from a traditional .NET object. First, the Player object derives from the DependencyObject. This is necessary to implement DependencyProperty properties. Declaring an object as a DependencyObject informs the system that the object wants to utilize the built-in features defined earlier.

Second, several of the properties are defined as DependencyProperty items. When providing a DependencyProperty to back a property, the property is exposed to the dependency system, which automatically gives the powerful features defined previously. You can utilize both traditional properties and DependencyProperty items within a DependencyObject. However, only the DependencyProperty items will gain the pre-defined built-in features.

After defining the source (the Player object), the target item can bind, or interact, with the source. At this point, the target has not been defined. This example creates a simple player profile page, which will display a player's first name, last name, and position. Here is the XAML code for the window that will display this information:

<Window x:Class="Source_Code.Profile"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:AppCode="clr-namespace:Source_Code"
   Title="Player Profile" Height="300" Width="300"
   >
   <StackPanel Name="ProfileStackPanel" Orientation="Horizontal">
   <TextBlock Name="LastNameTextBlock" />
   <TextBlock Text=", " />
   <TextBlock Name="FirstNameTextBlock" />
   <TextBlock Text=" - " />
   <TextBlock Name="PositionTextBlock" />
   </StackPanel>
</Window>

Placing this code into XAMLPad simply displays a ", – ". This is because the goal is to bind the page fields to the properties of a player. To set up the binding, the XAML code must be edited. The following XAML code shows the profile being populated from an object. The additions made to the previous code sample are formatted in blue.

<Window x:Class="Source_Code.Profile"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:AppCode="clr-namespace:Source_Code"
   Title="Player Profile" Height="300" Width="300"
   >
   <StackPanel Name="ProfileStackPanel" Orientation="Horizontal">
      <StackPanel.Resources>
         <AppCode:Player x:Key="profile" FirstName="/img/2006/12/Chad"
                         LastName="Campbell" Position="WR" />
      </StackPanel.Resources>
      <TextBlock Name="LastNameTextBlock"
                 Text="{Binding Path=LastName}" />
      <TextBlock Text=", " />
      <TextBlock Name="FirstNameTextBlock"
                 Text="{Binding Path=FirstName}" />
      <TextBlock Text=" - " />
      <TextBlock Name="PositionTextBlock"
                 Text="{Binding Path=Position}" />
      <StackPanel.DataContext>
         <Binding Source="{StaticResource profile}" />
      </StackPanel.DataContext>
      </StackPanel>
</Window>

To implement binding, the first step was defining the source to be bound to. This definition occurs within the <StackPanel.Resources /> element at the line that begins with <AppCode:Player. This line instantiates an instance of the Player class and assigns the values of the objects properties, which demonstrates the ability to define objects via markup code within WPF. (Other items can be placed within the resources section, but the preceding example implements only a data source.)

Next, the mappings between the object's properties (the Player's properties) and how to display them is implemented. This occurs in the Text attributes, which define which object (Player) property to use with the field. This is accomplished through the Binding markup extension. The Binding keyword declares the mapping through a series of comma-delimited name/value pairs.

Finally, the <StackPanel.DataContext /> element defines the data, or information scope, that the child elements of the StackPanel will consume. This could have been bypassed by appending an additional name/value pair within the Binding definition for each Text attribute.

Data-Binding Boosters

Data binding provides many ways to improve overall code quality and minimize maintenance tasks. The addition of the DependencyObject and the DependencyProperty extends data-binding benefits and adds significant power with minimal code adjustments.

Download the Code

Download the code for the example application.

MSDN Resources

About The Author

Chad Campbell (MCSD, MCTS) is a solutions developer for mid- to large-sized organizations. He is a thought leader specializing in Web-based solutions for Crowe Chizek in Indianapolis, Indiana. Reach him at cacampbell@crowechizek.com.




Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel