c# - Bind Bool to Visibility of TextBlock within a ListBox -
i want bind visibility of textblock, within listbox, bool value in viewmodel. binding works textblock outside listbox, isn't working textblock within listbox. please help!
xaml code:
<textblock x:name="heading" visibility="{binding myvb.visible, converter={staticresource booltovisconverter}}" width="480"/> <listbox x:name="lstbani1" itemssource="{binding users}"> <listbox.itemtemplate> <datatemplate> <stackpanel orientation="vertical"> <textblock x:name="tb1" text="{binding string1}" visibility="{binding myvb.visible, converter={staticresource booltovisconverter}}" width="480"/> <textblock x:name="tb2" text="{binding string2}" width="480"/> <textblock x:name="tb3" text="{binding string3}" width="480"/> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox> cs code:
public partial class page1 : phoneapplicationpage { public page1() { viewmodel model = new viewmodel(); model.users = getusers(); model.myvb = new myvisibility(); model.myvb.visible = false; this.datacontext = model; } // view model public class viewmodel { public list<user> users { get; set; } public myvisibility myvb { get; set; } } // bool visibility converter public class booleantovisibilityconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { return (bool)value ? visibility.visible : visibility.collapsed; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new notimplementedexception(); } } // property classes public class user { public string string1 { get; set; } public string string2 { get; set; } public string string3 { get; set; } } public class myvisibility : inotifypropertychanged { private bool _visible; public event propertychangedeventhandler propertychanged; public bool visible { { return _visible; } set { _visible = value; notifypropertychanged("visible"); } } public void notifypropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } } private list<bani> getusers() {....} }
your listbox bound users, therefore each item in list has datacontext bound user. therefore binding trying property in user class, not parent data context.
give page name , change binding following:
visibility="{binding datacontext.myvb.visible, elementname=yourpagename, converter={staticresource booltovisconverter}}"
Comments
Post a Comment