views:

80

answers:

2

Hi,

I have 2 Datatemplates. One contain a grid , second one contain a button. I need to send command parameters of button as selected grid items.

How can i do this ?

<ObjectDataProvider x:Key="Datas" ObjectType="{x:Type ViewModel:UserControlViewModel}"></ObjectDataProvider>

<DataTemplate x:Key="SourceGrid">
<WPFToolKit:DataGrid x:Name="SourceDataGrid" ItemsSource="{Binding Source={StaticResource Datas},Path=SourceGridData}" CanUserSortColumns="True" GridLinesVisibility="None" IsSynchronizedWithCurrentItem="True" SelectionUnit="FullRow"></WPFToolKit:DataGrid>
</DataTemplate>

<DataTemplate x:Key="AddRemoveDataTemplate">
<StackPanel>
<Button Name="Add" Content="Add">
<Button.Command>
<Binding Source="{StaticResource Datas}" Path="AddCommand"> 
</Binding>
</Button.Command>
<Binding ElementName="SourceDataGrid" Path="SelectedItem"></Binding>
</Button.CommandParameter>
</Button>
<StackPanel>
</DataTemplate>
A: 

Take a look at this article. Maybe it'll help you:

http://www.dev102.com/2008/08/07/how-to-access-a-wpf-control-which-is-located-in-a-datatemplate/

yossharel
I'm not looking for a code-behind solution.There is some issue in referring the "SourceDataGrid" in the following way...thats what i'm looking for<Binding ElementName="SourceDataGrid" path="SelectedItem"></Binding>
Anish
+1  A: 

You could try to use a Binding with the RelativeSource property set to the FindAncestor mode and looking for a DataGrid object. However, I am not sure whether it will work in your scenario because I do not know how these DataTemplates are related to each other. Is the second DataTemplate used for the items in the DataGrid?!

Somehow, your design feels strange to me. Are you sure that you need DataTemplates in both cases? What exactly do you want to achieve?

gehho
I am having 3 datatemplates 1- source grid.2- add/remove buttons3 - destination gridI have one question - DataGrids's selected items is a dependency property. Can I assignn it to a view-model declared property ?
Anish
This is fine. But what does "source grid", "add/remove buttons", and "destination grid" *mean* and what do they *do*? You know, you need to provide some more details, otherwise we won't be able to help you. Regarding `SelectedItems`: It is *not* a dependency property. Only `SelectedItem` (singular!) is a dependency property that you can use for data binding. However, you can use the Event-to-Command pattern or an attached property to enable data binding to the `SelectedItems` property. [Laurent Bugnion recently blogged about the former approach.](http://tinyurl.com/38kpof9)
gehho