wpf - Get TextBox Value in ViewModel -


anyone 1 please tell me example ,how textbox value in view model, model property binding textbox mode 2 way

enter image description here

this view model functions, want add new record observable collection.

c#

public void addperson() {   // add new record }  private model.person _persondata; public model.person persondata {     {         if(_persondata==null)         {             _persondata = new person();         }         return _persondata;         }     set     {         setproperty(ref this._persondata, value);     } } 

xaml

<controls:metrowindow     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:controls="clr-namespace:mahapps.metro.controls;assembly=mahapps.metro"     xmlns:vm="clr-namespace:demo.viewmodel"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:model="clr-namespace:demo.model" mc:ignorable="d" x:class="demo.mainwindow"     title="mainwindow" height="438" width="664"     glowbrush="{dynamicresource accentcolorbrush}"                   windowstartuplocation="centerscreen"> <controls:metrowindow.resources>     <model:person x:key="persondatasource" d:isdatasource="true"/>     <vm:mainviewmodel x:key="mainviewmodeldatasource" d:isdatasource="true"/> </controls:metrowindow.resources>     <controls:metrowindow.rightwindowcommands>     <controls:windowcommands>         <!--<button content="settings" />-->         <button>             <stackpanel orientation="horizontal">                 <rectangle width="20"                height="20"                fill="{binding foreground, relativesource={relativesource ancestortype={x:type button}}}">                     <rectangle.opacitymask>                         <visualbrush stretch="fill" visual="{staticresource appbar_futurama_fry}" />                     </rectangle.opacitymask>                 </rectangle>                 <textblock margin="4 0 0 0"                verticalalignment="center"                text="karthik" />             </stackpanel>         </button>     </controls:windowcommands> </controls:metrowindow.rightwindowcommands>   <grid datacontext="{binding source={staticresource mainviewmodeldatasource}}">     <grid.background>         <lineargradientbrush endpoint="0.5,1" startpoint="0.5,0">             <gradientstop color="#fff3f3f3" offset="0"/>             <gradientstop color="#fffbf9f9" offset="1"/>             <gradientstop color="#fff7f6f6" offset="0.555"/>         </lineargradientbrush>     </grid.background>     <border borderthickness="3" horizontalalignment="left" height="388" margin="10,10,0,0" verticalalignment="top" width="636" cornerradius="3">         <border.background>             <lineargradientbrush endpoint="0.168,0.166" startpoint="0.168,0.08">                 <gradientstop color="#ff39d5ff" offset="0"/>                 <gradientstop color="white"/>             </lineargradientbrush>         </border.background>         <label content="master details" margin="10,1,502,352" fontweight="bold" foreground="white"/>     </border>      <border borderbrush="#ffc7c2c2" borderthickness="1" horizontalalignment="left" height="238" margin="21,100,0,0" verticalalignment="top" width="187" cornerradius="3">         <border.background>             <lineargradientbrush endpoint="0.433,0.407" startpoint="0.435,0.134">                 <gradientstop color="#29c5ff" offset="0"/>                 <gradientstop color="white"/>             </lineargradientbrush>         </border.background>     </border>     <border borderbrush="#ffc7c2c2" borderthickness="1" horizontalalignment="left" height="239" margin="227,100,0,0" verticalalignment="top" width="405" cornerradius="3">         <border.background>             <lineargradientbrush endpoint="0.435,0.378" startpoint="0.435,0.134">                 <gradientstop color="#29c5ff" offset="0"/>                 <gradientstop color="white"/>             </lineargradientbrush>         </border.background>         <button content="delete" margin="211,189,41,21" width="149" height="21"/>     </border>     <label content="new" horizontalalignment="left" height="29" margin="37,103,0,0" verticalalignment="top" width="87" foreground="white"/>     <label content="update" horizontalalignment="left" height="25" margin="246,105,0,0" verticalalignment="top" width="98" foreground="white"/>     <textbox horizontalalignment="left" height="25" margin="37,150,0,0" textwrapping="wrap" text="{binding path=name,mode=twoway,updatesourcetrigger=propertychanged}" verticalalignment="top" width="149" datacontext="{binding source={staticresource persondatasource}}"/>     <textbox horizontalalignment="left" height="19" margin="37,197,0,0" textwrapping="wrap" text="{binding path=id,mode=twoway,updatesourcetrigger=propertychanged}" verticalalignment="top" width="149" datacontext="{binding source={staticresource persondatasource}}"/>     <button content="update" horizontalalignment="left" height="24" margin="37,291,0,0" verticalalignment="top" width="149"/>     <button command="{binding _addcommand}" content="edit" horizontalalignment="left" height="21" margin="272,291,0,0" verticalalignment="top" width="149"/>     <listview horizontalalignment="left" height="120" margin="246,150,0,0" verticalalignment="top" width="366"  itemssource="{binding masterdata}">         <listview.view>             <gridview>                 <gridviewcolumn  header="name" width="120">                     <gridviewcolumn.celltemplate>                         <datatemplate>                             <textblock text="{binding name}" textwrapping="wrap"/>                           </datatemplate>                        </gridviewcolumn.celltemplate>                     </gridviewcolumn>                 <gridviewcolumn header="id" width="120">                     <gridviewcolumn.celltemplate>                         <datatemplate>                             <textblock text="{binding id}" textwrapping="wrap"/>                         </datatemplate>                     </gridviewcolumn.celltemplate>                 </gridviewcolumn>                 <gridviewcolumn header="location" width="125">                     <gridviewcolumn.celltemplate>                         <datatemplate>                             <textblock text="{binding location}" textwrapping="wrap"/>                         </datatemplate>                     </gridviewcolumn.celltemplate>                 </gridviewcolumn>             </gridview>         </listview.view>     </listview>     <textbox horizontalalignment="left" height="19" margin="37,241,0,0" textwrapping="wrap" text="{binding path=location,mode=twoway,updatesourcetrigger=propertychanged}" verticalalignment="top" width="149" datacontext="{binding source={staticresource persondatasource}}"/>  </grid> </controls:metrowindow> 

since not use code snippet reproduce issue accurately. have made simple code sample achieve target described. believe useful information form , check code solve problem.

<grid>     <grid.columndefinitions>         <columndefinition>          </columndefinition>         <columndefinition>          </columndefinition>     </grid.columndefinitions>      <stackpanel grid.column="0">         <textbox text="{binding name,mode=twoway}"></textbox>          <textbox text="{binding id,mode=twoway}"></textbox>          <textbox text="{binding location,mode=twoway}"></textbox>          <button content="add new" command="{binding addnew}"></button>     </stackpanel>      <listview itemssource="{binding persons}" grid.column="1">         <listview.itemtemplate>             <datatemplate>                 <stackpanel>                     <textblock text="{binding name}"></textblock>                     <textblock text="{binding id}" margin="10 0 0 0"></textblock>                      <textblock text="{binding location}" margin="20 0 0 0"></textblock>                 </stackpanel>             </datatemplate>         </listview.itemtemplate>     </listview> </grid> 

mainviewmodel.cs

class mainviewmodel:viewmodelbase {     public observablecollection<personmodel> persons { get; set; }      private string _name;     public string name     {         { return _name; }         set         {             _name = value;             raisepropertychanged("name");         }     }      private string _id;      public string id     {         { return _id; }         set { _id = value;             raisepropertychanged("id");         }     }      private string _location;      public string location     {         { return _location; }         set { _location = value;             raisepropertychanged("location");         }     }      public relaycommand addnew { get; set; }       public mainviewmodel()     {         persons = new observablecollection<personmodel>();         persons.add(new personmodel() {name="test1",id="123",location="u.s." });         addnew = new relaycommand(addnewperson);     }      private void addnewperson()     {         personmodel p = new personmodel() {name=name,id=id,location=location };         persons.add(p);     } } 

personmodel.cs

class personmodel {     public string name { get; set; }      public string id { get; set; }      public string location { get; set; } } 

enter image description here


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -