c# - Why can't I set value from my Global static class to a TextBlock XAML -


here xaml :

<stackpanel grid.column="2" orientation="vertical" verticalalignment="center" horizontalalignment="left">                 <textblock  x:name="lbl1"  text="1"  margin="0,0,0,0"  fontsize="15" foreground="white" horizontalalignment="left" fontfamily="arial" verticalalignment="bottom" />                 <textblock  x:name="lbl1text" text="{x:static local:globals.currentuser.fullname}" grid.column="0" margin="0"  fontsize="16" foreground="white" horizontalalignment="left"  fontfamily="arial"  /> </stackpanel> 

as possible see i'm trying set logged in user being hold in static class in project.

on form load setting user globals.currentuser = loggedin();

and when try set user textblock because want display fullname who's logged in facing issue :

nested types not supported: globals.currentuser.fullname

this means can not access property fullname object currentuser? , how fix this, , why happening?

p.s know how in code behind :

lbl1.text = globals.currentuser.fullname; //and might work

but think "more right" approach bind textblock throught xaml

thanks guys cheers

that's because x:static syntax (quote documentation):

<object property="{x:static prefix:typename.staticmembername}" .../> 

where

typename: name of type defines desired static member.

staticmembername: name of desired static value member (a constant, static property, field, or enumeration value).

as see - can use type name , 1 member name. syntax global.currentuser.fullname not supported markup extension.

as workaround can use one-time binding this:

<textblock text="{binding source={x:static local:globals.currentuser}, path=fullname, mode=onetime}" /> 

if have more parts in path, globals.currentuser.person.fullname - still can use binding. since x:static supports 1 property - rest go in path:

<textblock text="{binding source={x:static local:globals.currentuser}, path=person.fullname, mode=onetime}" /> 

onetime binding not required practice , improves perfomance bit (though little of course). bindings used create relation between target , source (like update target when source changes , on). in case don't need that, need grab value source (static property) , assign target (text block text). onetime telling wpf binding , not bother trying find way listen source property changes (which not possible here anyway).


Comments