c# - Convert a simple WinForms app to WPF, specifically MVVM -
background: while created simple vb.net winforms app @ work consists of 1 form , 1 module. app reads , makes updates oracle database.
recently asked if learn c# wpf , mvvm when creating apps going forward. figured app simple enough try convert can learn when take on more challenging project. , have admit having difficult time. figured if mvvm part set correctly in @ least 1 part of app (the connecting/reading/verifying of employee id on database), experience enable me rest on own. don't have experience c# , don't know if got model , viewmodel correct here or if need reversed.
goal: want here type employee id in textbox , click button. if employee id exists, something. if not, display message box.
i started creating 3 folders in ide: model
, view
, viewmodel
. created model.cs
, mainwindow.xaml
, viewmodel.cs
.
i've made enough progress make public class verify
work. but, don't know if i've done correctly @ , behavior want isn't happening.
model.cs
namespace wpf_brazekiosk.model { public class model { } public class verify { public const string connectionstringtooracle = "provider=oraoledb.oracle.1;data source=mpcs;user id=secret;password=secret;"; public oledbdatareader rowread; public oledbdatareader emp_verify(string emp) { var conn = new oledbconnection(connectionstringtooracle); conn.open(); string query = "select employee_id mpcs.employee employee_id = ?"; var cmd = new oledbcommand(query, conn); cmd.parameters.addwithvalue("employee_id", emp); rowread = cmd.executereader(); return rowread; } } }
viewmodel.cs
namespace wpf_brazekiosk.viewmodel { public class viewmodel { private string _empid; public string empid { { return _empid; } set { if (_empid == value) { return; } _empid = value; } } } }
mainwindow.xaml
<window x:class="wpf_brazekiosk.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:wpf_brazekiosk" mc:ignorable="d" title="mainwindow" height="837" width="1000"> <grid> <textbox x:name="textbox2" height="23" margin="444,423,416,0" textwrapping="wrap" text="textbox" width="120" verticalalignment="top" horizontalalignment="center"/> <button x:name="button" content="remove" margin="369,612,351,0" width="260" height="124" fontsize="20" verticalalignment="top" horizontalalignment="center"/> </grid> </window>
you should add icommand
property view model. please refer following blog post more information commands , used for: http://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/. there lot more information mvvm , commands available online if search it.
once have done bind command
property of button
icommand
property of view model , text
property of textbox
empid
property:
<textbox x:name="textbox2" text="{binding empid}"/> <button x:name="button" content="remove" command="{binding yourcommand}"/>
...and implement logic in execute
method of command, e.g.:
public class viewmodel { public viewmodel() { yourcommandproperty = new delegatecommand(check); } private string _empid; public string empid { { return _empid; } set { if (_empid == value) { return; } _empid = value; } } public icommand yourcommandproperty { get; } private void check(object o) { //look empid here... messagebox.show("..."); } }
don't forget set datacontext
of view instance of viewmodel
class:
public mainwindow() { initializecomponent(); datacontext = new viewmodel(); }
Comments
Post a Comment