Dec 7, 2010 at 11:56 PM

I'm not sure if I'm doing something wrong here, but I can't figure out how to get the availability to update.

This is in SL4.


            <TextBox Width="175" x:Name="SearchText" />
            <Button x:Name="Search" Content="Search" />



private string _searchText;
        public string SearchText
            get { return _searchText; }
                _searchText = value;
                NotifyOfPropertyChange(() => SearchText);

 public bool CanSearch()
           return !string.IsNullOrEmpty(SearchText);

        public void Search()


CanSearch is called when the view is loaded, or anytime it is reshown again. It does not however get called when the SearchText has changed. What do I need to do to get this to work?



Dec 8, 2010 at 12:15 AM

I think you need to add a


NotifyOfPropertyChange(() => CanSearch);

after the SearchText change notification, ottherwise there is no way the availability is updated.


Dec 8, 2010 at 12:20 AM

Yes, I tried that but it didn't work. What does work however is this:


        public bool CanSearch
                return !string.IsNullOrEmpty(SearchText);


Changing it into a property works property with NotifyOfPropertyChange, when it was a method, that didn't work of course.

Dec 8, 2010 at 12:59 AM

Yes, I had to add that the CanSearch should be a property to make it work.

If I am correct, if you want proper re-evaluation you need to implement the availability check as a property rather than a method.

Dec 8, 2010 at 1:24 PM

Yes. That is correct. You should really only use the method technique when the method has parameters. Then, CM watches for changes in parameter inputs and calls the method whenever the inputs change. On the whole, I recommend using properties and only falling back to the method technique when there's no other way to accomplish your goal. Using properties gives you complete control over the availability update.