Command Binding of a button inside a DataGridTemplateColumn #9240
-
Hi all! I'm struggling trying to bind a command to a button inside of a DataGridTemplateColumn. Having a look into my problem, I came across some links that I've tried without success:
This is the code of for the DataGridTemplateColumn <DataGridTemplateColumn Header="View">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ui:Button Appearance="Secondary" Icon="{ui:SymbolIcon Eye28}"
Command="{Binding Path=ViewSecretCommand, RelativeSource={RelativeSource AncestorType={x:Type local:SecretsPageViewModel}}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> and this is the command that I'm trying to bind, I'm using the MVVM CommunityToolkit: [RelayCommand]
private async Task OnViewSecret(SecretDto secret)
{
//TODO
await Task.CompletedTask;
} The error that I'm getting is the following:
For sure I'm missing something but not clear what is it. Please find the entire code here Many thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Try the following methods: A
<Button Command="{Binding ElementName=Root, Path=DataContext.ViewSecretCommand}" .../> B
<Button Command="{Binding DataContext.ViewSecretCommand, RelativeSource...}" .../> Some links that might help: |
Beta Was this translation helpful? Give feedback.
Try the following methods:
A
x:Name
yourPage
orWindow
, "Root" for example.DataContext
:B
AncestorType
, target a control that is outside of theDataTemplate
s. I guess in your case, it's theItemsControl
.SecretsPageViewModel
is in aDataContext
. So, your binding should be:Some links that might help: