I have a UserControl, it contained bool property A. In main window, which contain that UserControl, I have to enable/disable a button depends on the value of A. I tried to make A as public and binding button like this:
<Button IsEnabled="{Binding MyUserControl.A}"/>
And in UserControl, I set PropertyChangedEventHandler for Property A like:
private bool _a;
public bool A
{
get
{
return _a;
}
set
{
if (_a == value)
return
_a = value;
OnPropertyChanged("A");
}
}
It look fine. But I have no idea why it does not work. It seems that I have lack of some implementation to communicate between main window and its usercontrol (because with OnPropertyChanged, all binding inside usercontrol work well).
I have some solution for that are 1. Use Messenger to send a message from Usercontrol with the content is value of A, main control will catch & set value to IsEnabled of button. 2. Make a event & raise it anytime value of A is changed.
Do you have any idea about this problems and how to fix it? Do you think 2 below solutions will work well or you have any other recommendation?
Thanks for reading.
<< Edit >> This issue is solved. It is my mistake when set the datacontext of usercontrol in code-behind and do not recognize that I already set them in datatemplate. --> So, duplication make 2 times initialization of viewmodel of usercontrol. --> somehow, it make the NotifyPropertyChange work incorrectly.
I'm sorry the title of this question is not suitable for this silly mistake. It seems that I went on the right way to solve the question on title. Thanks for your reading & your advices.