views:

529

answers:

2

In my SilverLight application, I have a property in my ViewModel called 'vmProperty' and a list called 'dgSource'.

I bind my dgSource to the datagrid as ItemsSource at which point each datagrid row's datacontext changes to each item in dgSource. One of the columns, say a checkbox column, needs to bind to vmProperty. But since the ViewModel is no longer the row's datacontext, I cannot get access to this property.

How do I get around this problem? If the question is not clear, please let me know and I will post a sample. Thanks in advance.

A: 

Try to set checked property of your chechbox column to that:

{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YourViewClassName}}, Path=DataContext.vmProperty}

That's mean that parent element which type is your view type will be found and than vmProperty of it's DataContext will be retrieved as value.

UPDATE:

It was solution for WPF.

For silverlight I think you can try to use construction like this:

{Binding Path=DataContext.vmProperty, ElementName=YourElement}

Where DataContext of YourElement is instance of your view model. I think it can be a grid for example.

<Grid x:Name="YourElement" DataContext={Binding}>
<!-- DataGrid here -->
</Grid>
bniwredyc
I think that is WPF, isnt it? SilverLight does not have RelativeSource or AncestorType, I believe.
etrast81
It has RelativeSource but only in very limited capacity
AnthonyWJones
Sorry, it's WPF.
bniwredyc
A: 
AnthonyWJones
Thanks for your responses. I used the ElementName method you have mentioned. But I run into the same issue as in 'http://stackoverflow.com/questions/1089650/silverlight-datagrid-celltemplate-binding-to-viewmodel'. I put this outside the datagrid and this piece code works great. But within the datagrid it doesnt. Anyways, I used the link provided in that thread 'http://blogs.msdn.com/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx', but that seems to be relevant only for WPF. I am unable to access AddOwner or OverrideMetadata. Thoughts?
etrast81
Thanks Anthony, Managed to expose it as part of dgSource. Not happy about it as it is replication. But it is working. So... :)
etrast81
Thats the advantage of a __view__ model. It would be most unsatisfactory if it were simply the __model__ but the whole point of a view model is that its munged form of data that supports a specific view or task.
AnthonyWJones