data binding - databinding combobox in Dataform Silverlight using MVVM in Update -
i have master/detail – datagrid/dataform , after select item shows in dataform update, have problem databinding or populating combox departments , set selectedemployee.departmentid selectedvalue.
here 2 questions now:
1. in employeeviewmodel code doesn’t work , question why?
private observablecollection<department> _departments; public observablecollection<department> departments { { return _departments; } set { _departments = value; raisepropertychanged("departments"); } }
but code works fine
private observablecollection<department> _departments; public observablecollection<department> departments { { if (_departments == null) { _departments = new observablecollection<department> { new department() { id = 1, departmentname = "technical " + 1, }, new department() { id = 2, departmentname = "technical " + 2, }, new department() { id = 3, departmentname = "technical " + 3, } }; } return _departments; } set { _departments = value; raisepropertychanged("departments"); } }
2. behavior of combobox inside , outside dataform different. outside works, inside doesn’t. think here need use source in itemssource, don’t know how. there question how fix it?
employeeview.xaml
<navigation:page xmlns:local="clr-namespace:departmenttechmanager" x:class="departmenttechmanager.views.employeeview" 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" mc:ignorable="d" xmlns:navigation="clr-namespace:system.windows.controls;assembly=system.windows.controls.navigation" d:designwidth="820" d:designheight="780" title="employees" style="{staticresource pagestyle}" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:mvvmlightcmd="clr-namespace:galasoft.mvvmlight.command;assembly=galasoft.mvvmlight.extras.sl4" xmlns:data="clr-namespace:system.windows.controls;assembly=system.windows.controls.data" xmlns:dataformtoolkit="clr-namespace:system.windows.controls;assembly=system.windows.controls.data.dataform.toolkit" datacontext="{binding employeestatic, source={staticresource locator}}"> <data:datagrid grid.row="0" x:name="dgemployees" canusersortcolumns="true" isreadonly="true" autogeneratecolumns="true" itemssource="{binding employees}" selecteditem="{binding selectedemployee, mode=twoway}" margin="0,36,-123,0"></data:datagrid> <dataformtoolkit:dataform x:name="dfdetails" currentitem="{binding selectedemployee}" autogeneratefields="false" commitbuttoncontent="save" commandbuttonsvisibility="edit, commit, cancel"> <dataformtoolkit:dataform.edittemplate> <datatemplate> <stackpanel> <dataformtoolkit:datafield label="name"> <textbox text="{binding name, mode=twoway}" /></dataformtoolkit:datafield> <dataformtoolkit:datafield label="departments"> <combobox itemssource="{binding departments}" displaymemberpath="departmentname" selectedvaluepath="id" selectedvalue="{binding path=selectedemployee.departmentid, mode=twoway}" /> </dataformtoolkit:datafield> </stackpanel> </datatemplate> </dataformtoolkit:dataform.edittemplate> <i:interaction.triggers><i:eventtrigger eventname="editended"> <mvvmlightcmd:eventtocommand command="{binding saveemployeescommand}"/> </i:eventtrigger></i:interaction.triggers> </dataformtoolkit:dataform>
in viewmodellocator.cs:
public viewmodellocator() { _sp = serviceproviderbase.instance; createdepartment(); createemployee(); } #region employeeviewmodel private static employeeviewmodel _employee; public static employeeviewmodel employeestatic { { if (_employee == null) { createemployee(); } return _employee; } } [system.diagnostics.codeanalysis.suppressmessage("microsoft.performance", "ca1822:markmembersasstatic", justification = "this non-static member needed data binding purposes.")] public employeeviewmodel employee { { return employeestatic; } } public static void clearemployee() { //do later //_employee.cleanup(); _employee = null; } public static void createemployee() { if (_employee == null) { _employee = new employeeviewmodel(_sp.pageconductor, _sp.employeedataservice); } } #endregion #region departmentviewmodel private static departmentviewmodel _department; public static departmentviewmodel departmentstatic { { if (_department == null) { createdepartment(); } return _department; } } [system.diagnostics.codeanalysis.suppressmessage("microsoft.performance", "ca1822:markmembersasstatic", justification = "this non-static member needed data binding purposes.")] public departmentviewmodel department { { return departmentstatic; } } public static void cleardepartment() { //do later //_department.cleanup(); _department = null; } public static void createdepartment() { if (_department == null) { _department = new departmentviewmodel(_sp.pageconductor, _sp.departmentdataservice); } } #endregion
can me?
combox empty. can populate departments so:
departmenttechmanagerdomainservice.metadata.cs
[metadatatypeattribute(typeof(employee.employeemetadata))] public partial class employee { [include] public department department { get; set; } public nullable<int> departmentid { get; set; } public string name { get; set; } }
departmenttechmanagerdomainservice.cs
public iqueryable<employee> getemployees() {return this.objectcontext.employees.include("department").orderby(e=>e.name);}
here viewmodel code:
private observablecollection<department> _departments; public observablecollection<department> departments { { return _departments; } set { _departments = value; raisepropertychanged("departments"); } } private department _selecteddepartment; public department selecteddepartment { { return _selecteddepartment; } set { _selecteddepartment = value; raisepropertychanged("selecteddepartment"); } } private void initializemodels() { employees = new observablecollection<employee>(); selectedemployee = new employee(); newemployee = new employee(); //new departments = new observablecollection<department>(); selecteddepartment = new department(); } private void getemployeescallback(ienumerable<employee> employees) { if (employees != null) { foreach (var employee in employees) { employees.add(employee); //new if (!departments.contains(employee.department)) departments.add(employee.department); } if (employees.count > 0) { selectedemployee = employees[0]; } } }
i make departments distinct, here departments have been selected, here aren't haven't been selected yet, , still combobox not populated departments in dataform. ?!
2nd question - looks combobox
inside , outside of dataform receives different datacontext
, hence trying locate departments
properties on different sources. not clear how fix since haven't shown of viewmodels.
pay attention vs output
window, provides detailed info regarding binding errors , i'm assuming there binding errors in case.
try modifying department related bindings in following way:
<combobox itemssource="{binding datacontext.departments, relativesoruce={relativesource ancestortype={x:type localviews:employeeview}}}" />
where localviews
should xml-namespace departmenttechmanager.views
. try same trick selecteditem
binding.
Comments
Post a Comment