0

I am trying to bind a button withing datagrid to a custom command.

But for some reason, the command dont get executed.

This is the XAML:

    <Grid Name="LayoutRoot">
        <ListView Name="ListView1"  
          ItemsSource="{Binding}"
          IsSynchronizedWithCurrentItem="True">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="ID" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=id,Mode=OneWay}" 
                                 Margin="-6,0,-6,0" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Name" Width="100">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=name,Mode=OneWay}" 
                                 Margin="-6,0,-6,0" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Path" Width="50">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=path,Mode=OneWay}" 
                                 Margin="-6,0,-6,0" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Update" Width="50">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button CommandParameter="{Binding id}" Command="{Binding ???????????}" Content="Edit"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

What should I write instead of the ?????????? in order to show a message with the ID.


Found a solution here:

WPF DataGrid - Button in a column, getting the row from which it came on the Click event handler

Community
  • 1
  • 1
Uri Goren
  • 13,386
  • 6
  • 58
  • 110

2 Answers2

1

I think problem is in Cells DataContext. Within DataTemplate it does not see parent context. Try to add your command to Resources and do something like:

<DataTemplate>
    <Button CommandParameter="{Binding id}" Command="{StaticResource yourCommand}" Content="Edit"/>
</DataTemplate>
Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54
0

Create a command Inherit from ICommand interface.

public class ShowMessageWithIdCommand : ICommand
{
// Implement it members
}

in your viewmodel:

public ICommand ShowMessageWithIdCommand
{
   get{return new ShowMessageWithIdCommand(...);}
}

Then your ????? will be: ShowMessageWithIdCommand

More detail you can find here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

Thai Anh Duc
  • 524
  • 6
  • 12