c# - Is there a XAML equivalent to nameof? -
i'm working devexpress's wpf tree list view , came across think more general problem relating renaming properties on objects used item source. in tree list view 1 required specify parentfieldname , keyfieldname (which used determine structure of tree). these fields strings.
this has led issues refactoring code. example renaming property of objects using itemsource break tree view parentfieldname , keyfieldname no longer in sync property names. have worked around issue creating properties in view model "parentfieldname" , "keyfieldname" use nameof present property name view.
here cut down version of control:
<usercontrol xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" d:designheight="300" d:designwidth="300"> <usercontrol.datacontext> <viewmodel /> </usercontrol.datacontext> <dxg:treelistcontrol autogeneratecolumns="addnew" enablesmartcolumnsgeneration="true" itemssource="{binding results}" selectionmode="row"> <dxg:treelistcontrol.view> <dxg:treelistview parentfieldname="{binding parentidfieldname}" keyfieldname="{binding nodeidfieldname}" showhorizontallines="false" showverticallines="false" shownodeimages="true"/> </dxg:treelistcontrol.view> </dxg:treelistcontrol> </usercontrol>
and viewmodel:
using devexpress.mvvm; public sealed class viewmodel : viewmodelbase { public string parentidfieldname => nameof(treenode.parentid); public string nodeidfieldname => nameof(treenode.nodeid); public observablecollection<treenode> results { => getproperty(() => results); set => setproperty(() => results, value); } }
and tree node:
public sealed class treenode { public int parentid {get; set;} public int nodeid {get; set;} }
my solution works wondering if there better way of doing this. example, there can in xaml equivalent nameof call, rather binding parentidfieldname , nodeidfieldname in view model?
i realize described issue devexpress's control. i'm interested in whether approach i've used around can improved on. there way in more simple way directly in xaml?
i apologize in advance if code i've provided doesn't compile. i've cut down i'm working quite considerably provide example.
you can create custom markup extension.
for example:
[contentproperty(nameof(member))] public class nameofextension : markupextension { public type type { get; set; } public string member { get; set; } public override object providevalue(iserviceprovider serviceprovider) { if (serviceprovider == null) throw new argumentnullexception(nameof(serviceprovider)); if (type == null || string.isnullorempty(member) || member.contains(".")) throw new argumentexception("syntax x:nameof type={x:type [classname]} member=[propertyname]"); var pinfo = type.getruntimeproperties().firstordefault(pi => pi.name == member); var finfo = type.getruntimefields().firstordefault(fi => fi.name == member); if (pinfo == null && finfo == null) throw new argumentexception($"no property or field found {member} in {type}"); return member; } }
sample usage:
Comments
Post a Comment