WPF MVVM Architecture Step by Step (4) (Add bindings-Completely remove background code)

Source: Internet
Author: User

Tag: Lock is info txt and XML minus HTTPS BSP

Original: WPF MVVM schema Step by Step (4) (Add bindings-Completely remove background code)

The previous improvements have been pretty good, but we now know the background code problem, it is possible to remove the background code all? This is what the WPF binding and commands are doing.

WPF is known for its ultra-binding,commans and declarative programming. Declarative programming means that we can also use XAML to represent C # code compared to all written in C # code. Binding can help us connect 2 different WPF objects to send and receive data.

You see there are 3 steps to map the C # code now:

    • Introduction : The first thing to do first is to introduce the Customerviewmodel namespace.
    • Create object : The second thing is that we create an object of the Customerviewmodel class.
    • binding Code : Finally, we bind the WPF UI and the view model object together.

Here is a table for showing C # code and XAML Code

Import C # code XAML Code
Import Using Customerviewmodel; Xmlns:custns= "clr-
Namespace:customerviewmodel;assembly=custo
Merviewmodel "
Create
Object
Customerviewmodelobj = new
Customerviewmodel ();
Obj. CustomerName = "Shiv";
Obj. Amount = 2000;
Obj. married = "Married";
<Window.Resources><custns:CustomerViewModel x:Key="custviewobj" TxtCustomerName="Shiv" TxtAmount="1000" IsMarried=”true”/>
Bind Lblname.content = O.customername;
<Label x:Name="lblName"  Content="{Binding TxtCustomerName, Source={StaticResourcecustviewobj}}"/>

You do not need to write the binding code in the background, we can choose a UI element, press F4 to select the properties to bind, this step will put the code into the XAML, of course, you can also write your own handwriting.

When specifying the mapping you can select StaticResource and then specify the bindings between the view model and UI elements.

  

If you look at the backstage code in Xaml.cs, you will find that there is no glue code at all, and no code to convert and map. The only code that WPF uses is the canonical code that initializes the WPF UI.

 Public Partial class Mainwindow:window    {        public  MainWindow ()        {            InitializeComponent ();        }           }

WPF MVVM Architecture Step by Step (4) (Add bindings-Completely remove background code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.