I’m using Silverlight with the MVVM.
I have a simple LoginControl bound to a LoginControlViewModel. The LoginControl is created and added to my MasterPage.
When the user logs in to my app, by pressing the Login BUtton on the LoginControl, the LoginControlViewModel sends an event back to the MasterPageControlViewModel. At this point the MasterPageControlViewModel.LoginVisible property is set to False.
The problem i am having is with XAML of the MasterPageView, since I dont know how to bind the LoginControl to the MasterPageControlViewModel.LoginVisible property. The below doesn’t work.
<Controls:Login x:Name="LoginControl" Style="{StaticResource LoginControlStyle}"
Visibility="{Binding LoginControlVisibility, Converter={StaticResource BoolConverter}}" />
The output window states the following:
System.Windows.Data Error: BindingExpression path error: ‘LoginControlVisibility’ property not found on ‘Silverlight.Controls.LoginControlViewModel’ ‘Silverlight.Controls.LoginControlViewModel’ (HashCode=43749873). BindingExpression: Path=’LoginControlVisibility’ DataItem=’Silverlight.Controls.LoginControlViewModel’ (HashCode=43749873); target element is ‘Controls.Login’ (Name=’LoginControl’); target property is ‘Visibility’ (type ‘System.Windows.Visibility’)
Any idea how to resolve this?
Ah, and I see your property is on a different ViewModel than what the control has for it’s DataContext.
What I’ve done in the past is place the
LoginControlin another container (say aBorderorStackPanel) and then you can bind theVisibilityof that panel to your property. That way the DataContext of theLoginControlstays theLoginControlViewModeland the visibility binding is simple.Edit: Turns out below method is not supported in Silverlight.
Or you can have a complicated binding on the
LoginControlthat looks at its parentsDataContext:Also isn’t your property called
LoginVisiblenotLoginControlVisibilityas you have in your binding? Or was that a typo just in the question?