【问题标题】:UWP ButtonColumn MvvmLight / Telerik / WindowsTemplate StudioUWP ButtonColumn MvvmLight / Telerik / WindowsTemplate Studio
【发布时间】:2020-06-22 07:10:28
【问题描述】:

我正在尝试将 ButtonColumn 添加到由 Windows 模板工作室生成的 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 案例中复制它。

【问题讨论】:

    标签: c# telerik uwp-xaml windows-template-studio


    【解决方案1】:

    恐怕你不能使用 x:bind 绑定OpenCustomerCommand in DataTemplate,一般来说,如果我们想绑定viewmodel的OpenCustomerCommand,我们需要将当前页面DataContext设置为ViewModel然后使用绑定标记扩展进行绑定,如下所示。

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

    这是您可以参考的similar 案例。

    【讨论】:

    • 对不起,我没有让它运行。我将当前页面 DataContext 定义为 ViewModel,如下所示:
    • 请给根面板一个 x:name 并绑定 ElementName="panel name"。
    • 我做到了,给 Page、Grid、DataGrid 一个自己的 x:Name 并尝试了所有这些。如果我在 Path=DataContext.OpenCustomerCommand 上按 F12,我会到达 CustomerListViewModel 中的命令,但在 Debug 中它仍然没有功能,也没有达到 BreakPoints。 :((
    • @Keke,可能您需要将命令插入数据源,请参考此案例link
    • 我真的不知道如何弄清楚。但是让它与 ListView 一起运行,你在 stackoverflow 上的其他问题的答案。感谢您的帮助,但无法将其标记为已解决,我认为必须有一种方法可以使用 DataGrid 运行它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多