c# - MVVM - DependencyProperty - Binding a String to Textbox - PropertyChanged event only fired up if mouse clicks on another element -


i playing first mvvm apps..

and found crazy behavior, did not know how solve it.

when start test app, button enabled.

now click in textbox, delete text in it..

button still enabled. click anywhere else, nothing changes..

click on button, button changes disabled cause canexecute false !

click in textbox , enter text, button still disabled..

in case button never come enabled cause cannot click on other element??!

here xaml:

<window x:class="tests.view"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:my="clr-namespace:tests"     title="view" height="109" width="156"     > <grid>     <button         x:name="btn"         content="refresh"         horizontalalignment="left"         margin="10,10,0,0"         verticalalignment="top"         width="75"         command="{binding refreshcmd}" />     <textbox         x:name="txtbox_one"         text="{binding teststring}"         horizontalalignment="left"         height="23"         margin="10,37,0,0"         textwrapping="wrap"         verticalalignment="top"         width="120"/> </grid> 

here code:

namespace tests {     public class viewmodel : dependencyobject     {         public string teststring         {             { return (string)getvalue(teststringproperty); }             set { setvalue(teststringproperty, value); }         }          public static readonly dependencyproperty teststringproperty=             dependencyproperty.register("teststring", typeof(string), typeof(viewmodel), new propertymetadata("huhu"));           public viewmodel ()         {             this.refreshcmd=new relaycommand(e => refreshexec(), c => this.canexecuterefreshcmd());         }          public icommand refreshcmd { get; internal set; }          private bool canexecuterefreshcmd ()         {             if (teststring.isnotnullorempty()) return true;             return false;         }          int cnt=0;         public void refreshexec ()         {             teststring=cnt++.tostring();         }     }      public partial class view : window     {         public view ()         {             initializecomponent();              this.datacontext=new viewmodel();         }      } } 

try set binding's updatesourcetrigger propertychanged :

<textbox     x:name="txtbox_one"     text="{binding teststring, updatesourcetrigger=propertychanged}"     horizontalalignment="left"     height="23"     margin="10,37,0,0"     textwrapping="wrap"     verticalalignment="top"     width="120"/> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -