Simplified architecture of my Silverlight app:
- MainPage; DataContext set to MainViewModel
- MainPage has two elements: UserControl and Rectangle
- in MainViewModel, I have two properties, UserControlVisible and RectVisible, both of type Visibility, binded to Visibility properties of those two elements in MainPage.XAML
- MainViewModel has INotifyPropertyChanged implemented
Problem is, when I set RectVisible property in MainViewModel to Visibility.Collapsed, Rectangle hides, which is fine, but when I set Visibility.Collapsed to UserControl (UserControlVisible property), it never hides!
I cannot hide that user control, and I have to do it through my ViewModel class. Why it works with Rectangle element, but not with UserControl? When I manually set Visibility to Collapsed in XAML, then it's hidden, but I have to do it through code and ViewModel object.
(edit) Temporary sollution:
I manually subscribed to PropertyChanged event in codebehind
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
viewmodel=new MainViewModel();
this.DataContext = viewmodel;
// fix for binding bug:
viewmodel.PropertyChanged += viewmodel_PropertyChanged;
}
void viewmodel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "LoginVisible")
loginWindowControl.Visibility = viewmodel.LoginVisible;
}
I had the same problem, and I fixed it and I don't know if my solution will work for you. The datacontext for my "MainPage" was different than the datacontext for my UserControl. I was setting the datacontext for my usercontrol manually through XAML I will give an example:
the VisibleProperty is this case should belong to the myusercontroldatacontext, and not the the datacontext where the parent xaml is defined
You likely need to find a way to bind the Visibility property to the root of your user control's visuals (the Grid/Panel).