DataGrid: how to bind a button
pcdus opened this issue · comments
Hello,
I use the DataGrid with some DataGridTextColumns and a DataGridTemplatedColumn that must contain a button allowing to navigate to another page.
In code-behind, there is no problem.
But when I try to use a ViewModel implementation with a Command, it doesn't work!
The code looks like this:
<controls:DataGrid x:Name="DataGrid" Grid.Row="1"
ItemsSource="{Binding MyForms}"
SelectionMode="Single"
SelectedItem="{Binding SelectedForm, Mode=TwoWay}"
DefaultOrderIndex="1">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn Width="0.7*" Header="Form Nr" Binding="{Binding form_id}" />
<controls:DataGridTextColumn Width="*" Header="Submitter" Binding="{Binding submitter}" />
...
<controls:DataGridTemplatedColumn Width="*" Header="Action">
<controls:DataGridTemplatedColumn.CellTemplate>
<DataTemplate>
<Button Content="Open">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Tapped">
<core:InvokeCommandAction Command="{Binding OpenFormCommand}" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Button>
</DataTemplate>
</controls:DataGridTemplatedColumn.CellTemplate>
</controls:DataGridTemplatedColumn>
</controls:DataGrid.Columns>
...
</controls:DataGrid>
Would you have an idea?
Do you have binging errors in your output? The context inside a column is the item not the root view model... You could bind to the parent view model via a static resource, see https://blog.rsuter.com/recommendations-best-practices-implementing-mvvm-xaml-net-applications/