wpf - TextBox's Text set through DataTrigger not updating the property value in Model -


i new wpf , want clear textbox's value if check box unchecked. tried doing through data triggers.

below code:

<textbox text="{binding path=amount,mode=twoway}">                     <textbox.style>                         <style>                             <style.triggers>                                 <datatrigger binding="{binding path=isselected}" value="false">                                     <setter property="textbox.text" value="{x:null}"></setter>                                 </datatrigger>                               </style.triggers>                         </style>                     </textbox.style>                 </textbox>  

my checkbox's value set in "isselected" property of model. here, if checkbox unchecked, text's updated value, {x:null} in case, not reflecting in "amount" property of model. because of text never seems changed on ui. "amount" earlier set value getting set again in textbox because of binding

any appreciated. let me know in case need more information or clarification thanks.

in such cases prefer viewmodel / model doing "clear" part of functionality,

hence in case i'd like:

public bool isselected {   {     return _isselected;   }    private set {     if (value == _isselected)       return;      raisepropertychanging(() => isselected);     _isselected = value;     raisepropertychanged(() => isselected);      if (_isselected == false)       amount = string.empty   } } 

that way view not hold responsibility logic , doesn't need datatrigger @ all

update:

the problem code when set text binding in textbox takes precedence on value set in style text property. can check using this:

<textbox>   <textbox.style>     <style targettype="{x:type textbox}">       <setter property="text"               value="{binding path=amount,                               mode=twoway}" />       <style.triggers>         <datatrigger binding="{binding path=isselected}"                       value="false">           <setter property="text"                   value="{x:null}" />         </datatrigger>       </style.triggers>     </style>   </textbox.style> </textbox> 

this clear text when checkbox checked, not updating binding(amount) since binding active when checkbox selected.


Comments