Wednesday, April 25, 2007

DataBinding In WPF

Today I will discuss about DataBinding that I have faced in my experience. please leave a comment and I'll cover them in my next post.

DataBinding is the technique of connecting controls and elements to the data. In the past, a programmer would write code both to initialize control from the variable and to set the variable into controls . In modern programing environments , the programmer defines a binding the control and variable , the binding automatically performs both the jobs.

Very often a data binding can replace an event handler, and simplifying the code. DataBinding are considered to have Source and Target. Here source is Data and Target is the Control and may be two controls will participate.

The bound property of the source need not be a Dependency property , but target property must be derived from the dependency object.

How can we bind data using {Binding} concept?
we can bind data between two elements or from resource collection. The following format is for bind with resource collections.

<!-- Resource Area-->
<Window.Resources>
<system:String x:Key="myvar">Hi , This is Binding </system:String>
</Window.Resources>

<!-- Bind With resource -->
//Method 1 (this method will bind whole object)
<TextBlock TextContent="{StaticResource myvar}" />

//Method 2 (this will bind particular property of object). it will display lenth property value of string .
<TextBlock Content="{Binding Source={StaticResource myvar}, Path=Length}" />

For Bind With Another Elements

<TextBlock Content="{StaticBinding Path=Length, ElementName=MyBox}" />
<TextBox Name="MyBox" />

Here textblock getting changes its content while typing the content on textbox.


Binding Modes

You can specify the mode property is separated from the path using comma.


Type of Mode

  1. OneWay -From the picture label get the value from source textbox. But it wont affect textbox value while the change happens in the label.

  2. TwoWay - From the picture Label get the from the Source textbox and also textbox will get the reflection while the change happens in the label.

  3. OneTime - From the picture Label initialized from the source textbox but does not track changes .

  4. OneWayToSource -if target property is not packed by a dependency property while source is . In that case we can use this mode.

DataContext


DataContext is inherited through the element tree , so if you set it for one element it is also applies to all the children of that element. For example if we set the binding in stackpanel. We can bound properties of all the control under stackpanel.

1 comment:

  1. In WPF, "What are the available options for setting relative path for an image or a file . In my scenario I have some image files ( resource files) and two XML files . I have put the path of the files settings.settings as
    pack://application:,,,/images/flag_green.gif
    pack://application:,,,/flights.xml
    and am calling the files using the following code
    global::ShortTermPlanningWPF.Properties.Settings.Default.FlightSchedulePath
    when am accessing an image file using the above path it is working fine , but when am trying to call an XML file it is throwing an error .
    Why it is showing such a strange behavior"


    Regards,
    Sree

    ReplyDelete