wpf - Silverlight treeview. Cannot bind "IsExpanded" property -


i have treeview control , want bind tree nodes' isexpanded property datasource items!

but have exception:

system.windows.markup.xamlparseexception occurred   message=set property '' threw exception.    stacktrace:        @ system.windows.application.loadcomponent(object component, uri resourcelocator)        @ silverlighttree.bstreeview.initializecomponent()        @ silverlighttree.bstreeview..ctor()   innerexception: system.notsupportedexception        message=cannot set read-only property ''.        stacktrace:             @ ms.internal.xamlmemberinfo.setvalue(object target, object value)             @ ms.internal.xamlmanagedruntimerpinvokes.setvalue(xamltypetoken intype, xamlqualifiedobject& inobj, xamlpropertytoken inproperty, xamlqualifiedobject& invalue)        innerexception:  

inner exception:

{system.notsupportedexception: cannot set read-only property ''. 

xaml:

<grid x:name="layoutroot">     <controls:treeview name="treeview" selecteditemchanged="treeview_selecteditemchanged"                        style="{binding  treeviewconnectinglines}" borderbrush="{x:null}">         <controls:treeview.itemtemplate>             <toolkit:hierarchicaldatatemplate itemssource="{binding children}">                 <stackpanel orientation="horizontal"  background="transparent">                     <toolkitdrag:contextmenuservice.contextmenu>                         <toolkitdrag:contextmenu loaded="contextmenu_loaded"                                                  opened="contextmenu_opened"/>                     </toolkitdrag:contextmenuservice.contextmenu>                     <image source="{binding path=type.icon}"  width="20" height="20" />                     <textblock text="{binding path=fulldescription}"   height="20"                                textalignment="center" horizontalalignment="center" />                 </stackpanel>             </toolkit:hierarchicaldatatemplate>         </controls:treeview.itemtemplate>         <controls:treeview.itemcontainerstyle>             <style targettype="controls:treeviewitem">                 <setter property="isexpanded" value="{binding isexpanded}"></setter>             </style>         </controls:treeview.itemcontainerstyle>          </controls:treeview> </grid> 

and data items:

public interface inode {     nodetype type { get; set; }     bool isselected { get; set; }     bool isexpanded { get; set; }     list<inode> children{get;set;}; } 

the quickest way subclass both treeview , treeviewitem, example:

public class bindabletreeviewitem : treeviewitem {     protected override dependencyobject getcontainerforitemoverride()     {         var itm = new bindabletreeviewitem();         itm.setbinding(treeviewitem.isexpandedproperty, new binding("isexpanded") { mode = bindingmode.twoway });          return itm;     } }  public class bindabletreeview : treeview {     protected override dependencyobject getcontainerforitemoverride()     {         var itm = new bindabletreeviewitem();         itm.setbinding(treeviewitem.isexpandedproperty, new binding("isexpanded") { mode = bindingmode.twoway });          return itm;     } } 

unfortunately you'll lose default theming of treeview when subclassing. weakness of silverlight theming concept. alternatively use custom attached property or behavior traverses tree , sets bindings outside. because tree nodes created lazily on-demand though, you'd have listen expanded event once each node has not yet been rendered, set bindings in event handler each of children after waiting layout pass.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -