Click here to Skip to main content
15,891,375 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF - basic simple dababinding to object

Rate me:
Please Sign up or sign in to vote.
2.50/5 (3 votes)
17 Nov 2010CPOL1 min read 9.4K   2   2
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 1 Google is your friend! http://www.go... Pin
Richard Deeming24-Nov-10 9:28
mveRichard Deeming24-Nov-10 9:28 
General1 Pin
Kevin Marois17-Nov-10 5:30
professionalKevin Marois17-Nov-10 5:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.