In my WPF application I have a button in each row of a data grid that is bound to a RelayCommand. If someone clicks the button in a row, the underlying method of the command is executed.
Unfortunately, this works just on the second click! The first time I click on the button in the row, only the row is selected. At the second click the command is executed.
If I select the row before, then it works on the first click on the button.
What am I doing wrong?
XAML:
<DataGrid AutoGenerateColumns="False"
IsReadOnly="True"
Style="{StaticResource DataGridStyle}"
ItemsSource="{Binding Path=Rows, Mode=OneWay}"
SelectedItem="{Binding Path=SelectedRow}">
<DataGrid.Columns>
<DataGridTextColumn Header="Number"
Width="Auto"
Binding="{Binding Path=Number}" />
<DataGridTemplateColumn Header= "Cancel"
Width="Auto" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding RelativeSource={RelativeSource
AncestorType=Expander},
Path=DataContext.CancelCommand}"
HorizontalAlignment="Center"
Width="20"
IsEnabled="{Binding IsCancelable}"
Visibility="{Binding RelativeSource={RelativeSource
AncestorType=DataGrid},
Path=DataContext.PermissionToWrite,
Converter={StaticResource
boolToVisibilityConverter}}"
>X</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>