c# - Setting DataContext in XAML in WPF -
i have following code:
mainwindow.xaml
<window x:class="sampleapplication.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525" datacontext="{binding employee}"> <grid> <grid.rowdefinitions> <rowdefinition height="auto" /> <rowdefinition height="auto" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="auto" /> <columndefinition width="200" /> </grid.columndefinitions> <label grid.row="0" grid.column="0" content="id:"/> <label grid.row="1" grid.column="0" content="name:"/> <textbox grid.column="1" grid.row="0" margin="3" text="{binding empid}" /> <textbox grid.column="1" grid.row="1" margin="3" text="{binding empname}" /> </grid> </window>
employee.cs
namespace sampleapplication { public class employee { public employee() { employeedetails employeedetails = new employeedetails(); employeedetails.empid = 123; employeedetails.empname = "abc"; } } public class employeedetails { private int empid; public int empid { { return empid; } set { empid = value; } } private string empname; public string empname { { return empname; } set { empname = value; } } } }
this simple code , want bind empid
, empname
properties in employee.cs class text properties of textboxes in mainwindow.xaml nothing appearing in these textboxes when running code. binding right?
this code fail.
as written, says: "look property named "employee" on datacontext property, , set datacontext property". isn't right.
to code work, is, change window declaration to:
<window x:class="sampleapplication.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:sampleapplication" title="mainwindow" height="350" width="525"> <window.datacontext> <local:employee/> </window.datacontext>
this declares new xaml namespace (local) , sets datacontext instance of employee class. cause bindings display default data (from constructor).
however, highly unlikely want. instead, should have new class (call mainviewmodel) employee
property bind to, this:
public class mainviewmodel { public employee myemployee { get; set; } //in reality should utilize inotifypropertychanged! }
now xaml becomes:
<window x:class="sampleapplication.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:sampleapplication" title="mainwindow" height="350" width="525"> <window.datacontext> <local:mainviewmodel/> </window.datacontext> ... <textbox grid.column="1" grid.row="0" margin="3" text="{binding myemployee.empid}" /> <textbox grid.column="1" grid.row="1" margin="3" text="{binding myemployee.empname}" />
now can add other properties (of other types, names), etc. more information, see implementing model-view-viewmodel pattern
Comments
Post a Comment