在日常的界面开发中,我们大多使用mvvm模式进行开发。通常情况下,一个propertygridcontrol或者datagrid的itemssource设置好,
然后每一列绑定好某一条itemssource中的某一个字段就可以跑起来了。
但是也有另一种情况:
假设一个界面temp.xaml,它的viewmodel为tempviewmodel.cs;
有一个propertygridcontrol的itemssource以observablecollection<model>绑定;
propertygridcontrol中的一个propertydefinition要重写template,它所绑定的信息并不只有model中的某个字段,
还可能包括model中的若干个字段,甚至tempviewmodel中的一些其它信息,这个时候该如何操作?
二、实例
temp.xaml:
<services:dockablepane.resources> <resourcedictionary> <datatemplate x:key="edittemplate"> <special:spedit x:name="part_editor"/> //这里是关键!!!!!!!!!!!!!!! </datatemplate> </resourcedictionary> </services:dockablepane.resources> <dxprg:propertygridcontrol margin="0" verticalalignment="stretch" horizontalalignment="stretch"selectedobjects="{binding infos}" showproperties="withpropertydefinitions"showdescriptionin="tooltipandpanel" showcategories="true" expandcategorieswhenselectedobjectchanged="true"showmenubuttoninrows="false" showtoolpanel="false" showsearchbox="false" sortmode="definitions"> <dxprg:propertygridcontrol.propertydefinitions> <!--通用--> <dxprg:propertydefinition isreadonly="true" path="code"/> <dxprg:propertydefinition isreadonly="true" path="aproperty"/> <dxprg:propertydefinition path="bproperty"/> <dxprg:propertydefinition path="cproperty"/> <dxprg:propertydefinition path="dproperty"/> <dxprg:propertydefinition path="eproperty" contenttemplate="{staticresource edittemplate}"/> </dxprg:propertygridcontrol.propertydefinitions> </dxprg:propertygridcontrol>
在这里,我们重写的datatemplate中的窗体名称为:part_editor
这个名字特别重要,不能改其它的。
这样我们就可以在spedit这个窗体中调用tempviewmodel的全部信息,因为这个时候tempviewmodel已经赋值给了spedit的datacontext的某个属性上,
可能的情况是这样的:
spedit.xaml.cs:
var source = this.datacontext as rowdata;if (source != null) _sourcedata = (source.definition.datacontext) as vm;
这样,我们就把temp.xaml的viewmodel传给了spedit的_sourcedata。
三、小结
本文主要描述了如何在重写界面中获取源ui中的viewmodel信息。part_editor是一个非常实用的隐藏方法。
以上就是分享part_editor的使用实例的详细内容。