Reputation: 11
Hope it's not a dup. I'm using Silverlight 4.
I have a collection MyProjects from my ViewModel MyVM binding to a datagrid MainDataGrid.
I also have a RowDetailsTemplate to show several buttons.
I have Command binding on these buttons. The command binding Command="{Binding Path=EditCommand}" appears working but I just can't get the CommandParameter binding working. Is Element name working inside a DataTemplate? What's the best way to pass in SelectedItem/SelectedDataRow via a CommandParameter binding inside a DataTemplate?
Thanks for your help.
<sdk:DataGrid x:Name="MainDataGrid" AutoGenerateColumns="False" DataContext="{StaticResource MyVM}" ItemsSource="{Binding MyProjects}" RowDetailsVisibilityMode="VisibleWhenSelected">
<sdk:DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Background="Ivory" Orientation="Horizontal">
<Button Style="{StaticResource DataGridRowDetailsButtonStyle}"
Command="{Binding Path=EditCommand}"
CommandParameter="{Binding ElementName=MainDataGrid, Path=SelectedItem}">Edit</Button>
Upvotes: 0
Views: 1633
Reputation: 39006
How about adding a property 'MySelectedItem' in the view model (where you define your EditCommand) and make it bound to your datagrid's selectedItem.
<sdk:DataGrid x:Name="MyDataGrid" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" SelectedItem="{Binding MySelectedItem}">
then you can probably do
CommandParameter="{Binding MySelectedItem}"
Upvotes: 1