我正在尝试将ButtonColumn添加到由Windows Template Studio生成的TelerikDataGrid中,而无需CodeBehind。 我认为,在一个完美的世界里,它会像这样工作。
<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>
这行不通,现在我尝试了很多机会,但从未达到 ViewModel。 我知道在 WPF 中它会使用
RelativeSource={RelativeSource AncestorType={x:Type UserControl},
但我没有在我的 UWP 案例中重现它。
恐怕不能用x:bind在DataTemplate
中绑定OpenCustomerCommand
,一般来说,如果要绑定视图模型的OpenCustomerCommand
则需要将当前页面DataContext
设置为ViewModel
然后使用绑定标记扩展进行绑定,如下所示。
<Button HorizontalAlignment="Right"
Margin="0,0,30,0"
Content="Favorite"
Command="{Binding ElementName=RootGrid,Path=DataContext.OpenCustomerCommand }"
/>
这是您可以参考的类似情况。