0

I need to bind a button's command inside a datatemplate like below:

                      <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="-" Cursor="Hand" Width="50"
                                            Background="Red" x:Name="removeButton"
                                            Command="{Binding Remove}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>

Unfortunately it does not work. How can I bind a command in a button insade a datatemplate?

I found that thread in the forum:

Bindings in nested WPF DataTemplates

but the method given by person, who answered this question, does not work as well. I think, that something has changed in WPF since this time, I would you grateful for your help.

1 Answers1

1

If Remove is defined in the view model of the parent ListView, you could bind to it using a RelativeSource:

Command="{Binding DataContext.Remove,
    RelativeSource={RelativeSource AncestorType=ListView}}"

You could also set the AncestorType to Window or UserControl depending on where the command property is defined and where the DataTemplate is applied.

mm8
  • 163,881
  • 10
  • 57
  • 88