views:

373

answers:

2

I've a Silverlight page using a MVVM behind it to handle all the data bits going on.

The data context is set for the page using:

DataContext="{Binding AddNewClientViewModel, Source={StaticResource ServiceLocator}}"

ServiceLocator being a service that allows me to create and inject the appropriate VM using an IoC container.

This all works fine.

Now I have a DataForm like so:

        <df:DataForm CurrentItem="{Binding NewClient}" AutoGenerateFields="False"  >
        <df:DataForm.NewItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <df:DataField>
                        <TextBox Text="{Binding ClientName}" />
                    </df:DataField>
                    <df:DataField>
                        <TextBox Text="{Binding Property_on_the_VM_not_on_NewClient}" />
                    </df:DataField>
                </StackPanel>
            </DataTemplate>
        </df:DataForm.NewItemTemplate>
    </df:DataForm>

OK, so this dataform binds to the NewClient property on my ViewModel. The first DataField binds to the NewClient.ClientName. The second DataField I'd like to bind to a property that hangs of the root ViewModel.

I know there is the 'Source' parameter that you can pass in when binding, if I had a static resource of the VM or similar I could point it to that, but I don't. How can I link this binding up with property on the parent VM?


Edit

After a post by Jobi below, I've tried the following:

<TextBox DataContext="{Binding DataContext, ElementName=root}"  Text="{Binding MyProperty}" />

And my top level control:

x:Name="root"
DataContext="{Binding AddNewClientViewModel, Source={StaticResource ServiceLocator}}"

No dice with getting this to work...

A: 

On the second TextBox you can do an ElementName binding to the root element where the Parent VM has already DataContext to.

<TextBox DataContent="{Binding DataContext, ElementName=rootLevelControl}" Text="{Binding property}"
Jobi Joy
Hi Jobi, this seems like it should work, but I'm not having any luck. I will update the question with my attempts at using this.
DaRKoN_
+1  A: 

What you need is a DataContextProxy which was created by Dan Wahlin. The problem is that once you get into the data form, you have a new data context. There is no easy way to reach back up to the view's data context. The data context proxy allows you to easily do this and I've used it quite a bit found it works great.

Bryant
That looks like the trick. Will implement and report back.
DaRKoN_