0

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?

3 Answers 3

1

I'm suggesting a variation based on the XAML you provided of the UserControl's DataContext binding:

 <TextBlock Text="{Binding SettingUserEdit.ApplicationVersion, Source={StaticResource Locator}}" /> 

I did not quite understand why you declared the DataContext binding for the UserControl in this way. Locator is declared in Resources either in App (recommended option) or in Window.
In both cases, the binding can be written as:

<UserControl ------------------------- ------------------------- DataContext="{Binding SettingUserEdit, Source={StaticResource Locator}}"> 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This worked nicely (apart from a missing curly bracket), and was the closest to what I was asking for
@GöranRoseen, I wrote here in the post editor. Missed an error. Thanks for showing her. I have corrected my answer.
1

If the StackPanel resides in the UserControl, you could bind to the DataContext of the parent UserControl using a {RelativeSource}

<TextBlock Text="{Binding Path=DataContext.ApplicationVersion, RelativeSource={RelativeSource AncestorType=UserControl}}" /> 

1 Comment

Thanks! I have seen some examples with "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType= ", but this syntax was a lot more elegant. I also didn't know what AncestorType to look for. This was definitely a good answer.
0

if you insist on changing DataContext for part of the view (which is not required), you can introduce additional container and change its DataContext:

<StackPanel x:Name="EditUserContainer"> <StackPanel DataContext="{Binding SelectedUser}" > <TextBlock Text="{Binding DisplayName}" /> <TextBlock Text="{Binding LastLoggedIn, StringFormat='Last login: \{0}'"/> </StackPanel> <TextBlock Text="{Binding ApplicationVersion, StringFormat='Application version: \{0}'"/> </StackPanel> 

personally I would use SelectedUser as part of binding path:

<StackPanel x:Name="EditUserContainer"> <TextBlock Text="{Binding SelectedUser.DisplayName}" /> <TextBlock Text="{Binding SelectedUser.LastLoggedIn, StringFormat='Last login: \{0}'"/> <TextBlock Text="{Binding ApplicationVersion, StringFormat='Application version: \{0}'"/> </StackPanel> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.