WPF two-way binding

Source: Internet
Author: User

Demand:

Ideas to save data in bulk.

Ideas:

Look at MVVM. Discover that only the foreground and background data can be synchronized. That is, the foreground text box content changes when the background object's properties also need to change.

Reference:

Http://www.cnblogs.com/luluping/archive/2011/05/06/2039475.html

The URL includes only a single object, and I need to bind the collection to the DataGrid, modify it on the DataGrid, and commit the whole batch. The idea is the same.

That is, the class must inherit from INotifyPropertyChanged

Then define the ViewModel, and the form and the ViewModel are tied up.

  public class person:inotifypropertychanged     {public        event PropertyChangedEventHandler propertychanged;        private string name;        public string name        {            get            {                return Name;            }            Set            {                name = value;                if (this. propertychanged = null)                {this                    . Propertychanged.invoke (this,new PropertyChangedEventArgs ("Name"));}}}}    

ViewModel

public class Personviewmodel    {public        list<person> Liperson        {            get;            Set;        }    }

Form

public partial class Mainwindow:window    {               Personviewmodel personviewmodel;                Public MainWindow ()        {            InitializeComponent ();            Personviewmodel = new Personviewmodel ();            Personviewmodel.liperson = new list<person> ();            person person = new person ();            Person. Name = "xxx";            PERSONVIEWMODEL.LIPERSON.ADD (person);            Person Person2 = new person ();            Person2. Name = "ASD";            PERSONVIEWMODEL.LIPERSON.ADD (Person2);                        This. DataContext = Personviewmodel;        }        private void Button_click_1 (object sender, RoutedEventArgs e)        {                      MessageBox.Show (personviewmodel.liperson[ 0]. Name);            MessageBox.Show (Personviewmodel.liperson[1]. Name);        }    }

Xaml

<window x:class= "Wpfstudy.mainwindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" x         mlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my= "Http://schemas.microsoft.com/wpf/2008/toolkit" Title= "MainWindow" height= "width=" 525 "> <Grid> <Grid.RowDefinitions> <row        Definition height= "></RowDefinition> <rowdefinition height=" 400* "></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <columndefinition width= "></" columndefinition> <columndefinition width= "200*" ></ColumnDefinition> </grid.columndefi nitions> <button content= "button" grid.row= "0" grid.column= "1" horizontalalignment= "left" verticalalignment= "Top" width= "click=" Button_click_1 "/> <textbox horizontalalignment=" left "grid.column=" 0 "height=" all "Te xtwrapping= "Wrap" text= "{Binding Path=name} "verticalalignment=" Top "width="/> <my:datagrid grid.row= "1" grid.column= "0" grid.c olumnspan= "5" autogeneratecolumns= "False" horizontalalignment= "left" name= "Dgsupg Ridview "itemssource=" {Binding path=liperson,mode=twoway} "Scrollviewer.horizontalscrollbarvisibilit                 y= "Auto" scrollviewer.verticalscrollbarvisibility= "Auto" > <my:DataGrid.Columns>            <my:datagridtextcolumn header= "name" isreadonly= "False" width= "+" binding= "{Binding Name}"/> </my:DataGrid.Columns> </my:DataGrid> </Grid></Window>

Students who are unfamiliar with MVVM can use this method to achieve bulk saving.

WPF two-way binding

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.