I am updating a project using MVVM Light. In the UserEditView, there is a list of users. Select one user, and the user data comes up in a StackPanel where it is editable.
To make things easier, the StackPanel sets its' DataContext to the SelectedUser property of the viewmodel. That means that all controls in the panel can just use {Binding <fieldname>}.
However, I have added a property to the viewmodel, and I would like to bind to it from within the panel. But I just can't find a way to do that binding. If I remove the DataContext= in the StackPanel, and prefix all bindings with "SelectedUser.", it all works nicely, but it is tedious and inelegant.
To summarize:
The viewmodel contains two bindable properties:
User SelectedUser; string ApplicationVersion; Then it is declared as a datasource in the view like this:
<UserControl.DataContext> <Binding Path="SettingUserEdit" Source="{StaticResource Locator}" /> </UserControl.DataContext> The view contains a lot of controls, and a bit down, we find:
<StackPanel x:Name="EditUserContainer" DataContext="{Binding SelectedUser}" > <TextBlock Text="{Binding DisplayName}" /> <StackPanel Orientation="Horizontal"> <TextBlock Text="Last login: " /> <TextBlock Text="{Binding LastLoggedIn}" /> </StackPanel> <StackPanel Orientation="Horizontal"> <TextBlock Text="Application version: " /> <TextBlock Text="{Binding Source={????}, Path=ApplicationVersion}" /> </StackPanel> ...where "Source={????}" is where I get stuck...
I have found a similar question with an accepted answer that seems really good: https://stackoverflow.com/a/1959701/1121033 The idea is to add the viewmodel as a resource with a key and then bind to it using Source={StaticResource key}. However, I can't get that to work, probably because my viewmodel is found using the viewmodel Locator as I show above, and whatever i write, it is not accepted syntax.
Anyone that can help med adapt that answer to my problem?