Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WPF - basic simple dababinding to object

0.00/5 (No votes)
17 Nov 2010 1  
A few days ago I was exploring the possibilities of WPF zažal coded. within a few days I started to use DataBindings. Nowhere on the Web site did not find any simple example of the use to upload DataBindings Object invoked from Codebehind.

Here is a simple example .. First you need to design the form.
Insert two TextBox elements. Two Lebel and one button.
The result will look like the picture ..

Form

then switches to the CS code file and create the following class bData

public class bData
    {
        Public bData (string val1, string val2)
        {
            this.val1 = val1;
            this.val2 = val2;
        }
        public string val1 {get, set;}
        public string val2 {get, set;}
    }


Class bData can take only two string values [ val1 and val 2]

Our task will be after you press button Load Values, to display data in textbox using DataBinding.

So switch to the XAML editor and modify the textbox text value
For the first texbox
Text = "{Binding Path = val1}"

to the second
Text = "{Binding Path = val2}"


hereby will get a object from the recorded value is displayed as text in val1 and val2 in texbox1 and textbox2

still need a code to the button. Then create method Click and write this line
this.DataContext bData = new ("test"; "test2");

This line load bData to current datacontext. When object is loaded. Textboxs take value by definiting of databining and show the values.


That's all. I hope that helps to understand simpliest usage of databinindg in WPF.


Happy coding :-\

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here