WPF-MVVM Learning Heart (WinForm turn to WPF Heart)

Source: Internet
Author: User

Contact with MVVM for nearly some time, have a little understanding, write it down.

Before it was done WinForm, work needed to learn WPF. The pros and cons are not to mention classes, online a lot. If I understand it myself, there are the following points:

1, first is the interface Xmal and interface separation: WPF also supports the drag control, but people who use WPF feel that writing controls in Xmal is a little bit more. And you can use static resources (Window. Resources) to set the style of each control, the uniform interface style is more convenient. The TargetType in the style specifies that a control belongs to a class, setter-specified property, and value.

such as <style x:key= "Txtboxstyle" targettype= "TextBox" >

<setter property= "Width" value= "/>"

</Style>

You can also add a template to the style, and then place more style templates.

2, data binding, can be said to be the core of MVVM. interface and background data interaction code, all placed in the VM (ViewModel), M (Model) placed data objects, such as the Order table in the SQL database, in Modle is an object class. V (View) is the interface layer.

Recently, a DataGrid data binding has been made, a TextBox, ComboBox, Button control is placed in the column, and the control is driven using data binding.

Using Datagridtemplatecolumn in the columns of the DataGrid, you can place controls such as a TextBox. and add the Textboxchanged event, reference (xmlns:ie= "http://schemas.microsoft.com/expression/2010/interactivity") to the TextBox,

<ie:interaction. Trigger>

<ie:eventrigger eventname= "TextChanged" >

<ie:invokecommandaction command= "{Binding string,mode=twoway,updatesourcetrigger=propertychanged}" commandparameter= "String"/>

</ie:interaction. Trigger>

You can use ElementName to pass any control to the VM layer directly in the button using command and Commandparameter,commandparameter. Of course this violates the MVVM design junior, but it can be used if necessary. How do you get information about other columns in the row where the button is located, such as the button in the row with the DataGrid selected? It is convenient to pass the DataGrid over and use the SelectedItem directly. Of course, it can also be passed to the VM layer in the V-layer. CS code.

When the DataGrid binds data, specify itemssource= "{Binding xxx}", use the Observablecollection<xxxmodel> collection in the VM layer, and set the onpropertychanged. The properties in the Xxmodel of the binding object in the DataGrid column are available.

This allows the data to be displayed in the DataGrid. Sometimes the data cannot be displayed after the binding, possibly the binding data object needs static or something. As binding in a ComboBox, this is the Set class ComboBox class, which has the value and text and GUID properties, and defines the collection in Xxxmodle (the DataGrid Data source object) and adds values to the collection. And the binding is written like this (don't ask why, the code is also found online): "{Binding xxxmodel.xxxcbbox},relativesource={relativesource mode=findancestor, Ancestortype=datagrid},mode=twoway,updatesourcetrigger=propertychanged} ".

The visibility property of a button can also be banding.

WPF-MVVM Learning Heart (WinForm turn to WPF Heart)

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.