0

I´m trying to add a ButtonColumn to a TelerikDataGrid which was generated by the Windows Template Studio, without CodeBehind. In a perfect world it would work like this, I think.

<tg:DataGridTemplateColumn x:Uid="Table_Open" >
    <tg:DataGridTemplateColumn.CellContentTemplate >
        <DataTemplate>
            <Button x:Uid="Button_Open" Command="{x:Bind ViewModel.OpenCustomerCommand}"></Button>
            </DataTemplate>
    </tg:DataGridTemplateColumn.CellContentTemplate>
</tg:DataGridTemplateColumn>

This doesn't work, now I tried many opportunities but never reach the ViewModel. I know in WPF it would work using

RelativeSource={RelativeSource AncestorType={x:Type UserControl},

But I don't get it reproduced in my UWP case.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Keke
  • 47
  • 7

1 Answers1

1

I'm afraid you can't use x:bind to bind OpenCustomerCommand in DataTemplate, In general, if we want to bind viewmodel's OpenCustomerCommand we need set current Page DataContext as ViewModel then use binding markup extension to bind like the following.

<Button HorizontalAlignment="Right" 
    Margin="0,0,30,0" 
    Content="Favorite" 
    Command="{Binding ElementName=RootGrid,Path=DataContext.OpenCustomerCommand }" 
       />

And this is the similar case that your could refer.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Sorry, i dont get it run. I definend current Page DataContext as ViewModel like this: – Keke Jun 22 '20 at 10:48
  • Please give root panel a x:name and binding ElementName="panel name". – Nico Zhu Jun 22 '20 at 12:23
  • I did, gave the Page, Grid, DataGrid an own x:Name and tried it with all. If i hit F12 on Path=DataContext.OpenCustomerCommand i reach the Command in CustomerListViewModel but in Debug its still without function, BreakPoints aren´t reached too. :(( – Keke Jun 22 '20 at 14:00
  • @Keke, may be you need to insert the command into datasource please refer this case [link](https://stackoverflow.com/a/61451757/7254781). – Nico Zhu Jun 22 '20 at 14:05
  • I realy don´t know how to figure it out. But got it run with ListView and your answers from other questions here on stackoverflow. Thanks for your help but can´t mark it as solved, i think there must be a way to get it run with DataGrid. – Keke Jun 22 '20 at 14:51
  • Yep, if the binding elementname can't work for datagrid, please try to move `OpenCustomerCommand` from viewmodel to each item. and it will works. – Nico Zhu Jun 22 '20 at 14:55