c# - How to do a simple XAML (WPF) conditional binding on the Visibility property -
i have got view model property:
public class mymodel { public bool isenabled {get;set;} }
i want use property toggle button state. if boolean true want hide button, , otherwise show it.
i tried things like:
<button visibility= "{binding isenabled ? hidden : visible }">enable</button>
but doesn't fit.
i tried more complex solution guess is, missing trivial.
any suggestions?
since want toggle between hidden
, visible
, true hidden can either write custom ivalueconverter
or use simple style.trigger
<button content="enable"> <button.style> <style targettype="{x:type button}"> <setter property="visibility" value="visible"/> <style.triggers> <datatrigger binding="{binding isenabled}" value="true"> <setter property="visibility" value="hidden"/> </datatrigger> </style.triggers> </style> </button.style> </button>
this assuming datacontext
set accordingly , mymodel.isenabled
raises inotifypropertychanged.propertychanged
event whenever changed
public class mymodel : inotifypropertychanged { private bool _isenabled; public bool isenabled { { return _isenabled; } set { _isenabled = value; onpropertychanged("isenabled"); } } #region inotifypropertychanged public event propertychangedeventhandler propertychanged; protected void onpropertychanged(string propertyname) { var handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } #endregion }
Comments
Post a Comment