poshgui.com

  • Home
  • Pricing
  • Documentation
    • Features | Roadmap
    • Changelog
    • Docs
    • Video Tutorials
  • Contact
  • About
  • Login
  • Home
  • Pricing
  • Documentation
    • Features | Roadmap
    • Changelog
    • Docs
    • Video Tutorials
  • Contact
  • About
  • Login

Docs

In-depth how-to’s on PoshGUI features

Popular Search textboxbuttonimagecombopicture

WPF

  • Starting New Project
  • Adding controls
  • Changing properties
  • Layout
  • Adding Events and Logic
  • DataContext
  • Exporting your project
  • Multithreading
  • Multipage Applications
  • Custom Lists
  • Controls
    • Tab Control
    • Grid
    • Border
    • StackPanel
    • ProgressBar

WinForms

  • Exporting your project
  • Starting New project
  • Adding controls
  • Changing Properties
  • Adding Events and logic

General

  • Cross-platform support
  • Code editors Keyboard Shortcuts
  • Why the UI on the site looks different than in runtime
  • Privacy Policy
  • Terms & Conditions
View Categories
  • Home
  • Docs
  • WPF
  • DataContext

DataContext

Data context is an additional layer between your logic and the user interface.

In other words, it keeps your UI separate from the logic, and data context is where the data lives.

It allows you to define your data types and their initial values in JSON format.

After binding them, when your application will have changed in the data, it will be automatically reflected in the UI.

Which significantly simplifies the process of app development

To understand data context properly, we will discuss data binding, setting up collection items, and taking input from users.

Video version of this tutorial

Data Binding #

Navigate to DESIGNER tab and open DATACONTEXT. You can see an editor here that allows you to feed initial data in JSON format.

Write the data into it as shown below:

{ "message1":"I am message 1", "message2":"I am message 2", "message3":"I am message 3" }
{ "message1":"I am message 1", "message2":"I am message 2", "message3":"I am message 3" }

Add controls in the designer, which are three textblocks in the example.

Select Common from the PROPERTIES tab. You can see a link icon adjacent to the field of Text.

Click on it and select the property you want to add to the control.

This is how each of the TextBlock will look after binding the adjacent data context field to them.

Adding collection Items #

Using Data Context, you can also add the items into collections like ListView, ComboBox, and DataGrid. To do so, the steps are as follows:

Add an array in the editor of DATACONTEXT with the name and initial values of your choice. (Remember, this is the initial data, and you can add/modify it when your application is running and the UI will update accordingly)

Update the DataContext with the following Items property:

{ "message1":"I am message 1", "message2":"I am message 2", "message3":"I am message 3", "Items" : ["l1","l2","l3","l4"] }
{ "message1":"I am message 1", "message2":"I am message 2", "message3":"I am message 3", "Items" : ["l1","l2","l3","l4"] }

Add the Listview control into the designer and from Common under the PROPERTIES tab, select the link icon adjacent to ItemsSource.

Select the Items property you would like to add to the ListView.

And you can see the data contexts mapped onto your ListView in just a few clicks.

Taking User Input #

You can utilize the data context to build two-way binding for taking user inputs as well. To do so, the following are the simple steps:

Add the field of your choice and assign it an empty value. For example, we have created ‘Name’ and ‘output’ with no data in them.

Setup the layout of your application by adding the required controls.

Bind the text property of the TextBox to one of the fields having an empty value. In this case, we have bound it with the ‘Name’.

Add a TextBlock to showcase output. Bind it to the field with an empty value as well. We have bound it with the ‘output’.

Add an event to the button under the EVENTS tab.

Navigate to the LOGIC tab and write the implementation of the function.

What will essentially happen now is that when the user will start writing in the textbox; the ‘Name’ property in Data Context will take on that data. And, after the click of the button, we will update the output property and display the name.

  1. Viola, it’s done!

(Remember to track the state of the particular data field, you will need to use $State.field_name  format as shown in the image.)

Accessing and changing data Context values during runtime #

For the understanding of $State, consider that during runtime, the Data Context is the $State object.

When you access and change its properties, the UI will automatically refresh.

You can also change the data for collections using the format

$State.collection += "I'm a new item!"
$State.collection += "I'm a new item!"

Still stuck? How can we help?

How can we help?

Updated on February 8, 2022
Adding Events and LogicExporting your project
Table of Contents
  • Data Binding
  • Adding collection Items
  • Taking User Input
  • Accessing and changing data Context values during runtime
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie settingsACCEPT
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the ...
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT