i trying bind visibility of control global variable:
i have visibility converter:
public class booleantovisibilityconverter : ivalueconverter { public bool isreversed { get; set; } public object convert(object value, type targettype, object parameter, string language) { return (value bool && (bool)value) ? visibility.visible : visibility.collapsed; } public object convertback(object value, type targettype, object parameter, string language) { return value visibility && (visibility)value == visibility.visible; } }
i created class inherits dependencyobject:
public class verified : dependencyobject { public static readonly dependencyproperty verifiedusernameproperty = dependencyproperty.register( "verifiedusername", typeof(string), typeof(verified), new propertymetadata(string.empty) ); public string verifiedusername { { return (string)getvalue(verifiedusernameproperty); } set { setvalue(verifiedusernameproperty, value); } } public static readonly dependencyproperty isverifiedproperty = dependencyproperty.register( "isverified", typeof(bool), typeof(verified), new propertymetadata(false, new propertychangedcallback(isverifiedchanged)) ); public bool isverified { { return (bool)getvalue(isverifiedproperty); } set { setvalue(isverifiedproperty, value); } } private static void isverifiedchanged(dependencyobject d, dependencypropertychangedeventargs e) { verified container = d verified; bool latestisverifiedvalue = (bool)e.newvalue; if (latestisverifiedvalue == true) { //to } } }
in app.xaml created variables:
public static verified myvalidator = new verified(); public static booleantovisibilityconverter myconverter = new booleantovisibilityconverter();
in simple page added text box , want bind it's visibility myvalidator variable.
in order accomplish creating binding in code:
binding mybinding = new binding(); mybinding.source = app.myvalidator; mybinding.path = new propertypath("isverified"); mybinding.mode = bindingmode.oneway; mybinding.updatesourcetrigger = updatesourcetrigger.propertychanged; mybinding.converter = app.myconverter; bindingoperations.setbinding(txtname2, visibilityproperty, mybinding);
in app.xaml: setting value of validator way :
app.myvalidator.isverified = false;
when navigate simple page text box not visible (ok).
to test behavior, added button on screen , added code change value of myvalidator.isverified.
as change value of app.myvalidator.isverified visibility of textbox not change. how can solve problem?
the binding system uses reflection property, not "field".
when declare
public static verified myvalidator = new verified();
you're declaring field, not property.
change myvalidator in app.cs property fix issue, below,
public static verified myvalidator { get; set; } = new verified();
update! reason works because accidentally declare verified property inside mainpage.cs,
public sealed partial class mainpage : page { public verified myvalidator1 {get; set;} // property name not matter public mainpage() { this.initializecomponent(); } }
not sure why works, i'm still looking it.
Comments
Post a Comment