最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mvvm - How to access command from vm from within a datatemplate? - Stack Overflow

programmeradmin1浏览0评论

I am upgrading my app to .NET 9 and previously used this to use commands from my page's vm in a datatemplate for listviews and such:

<ContentPage
    ...
    x:Name="this"
    x:DataType="viewmodel:MyViewModel">
    ...

    <DataTemplate x:DataType="model:MyClass">
        <Button
            Command="{Binding Source={x:Reference this}, Path=BindingContext.MyCommand}}"
            CommandParameter="{Binding .}" />
            ...

But now it seems due to the changes to compiled bindings, this no longer works. Does anyone know the new way of doing this? I've looked around online but all the answers are outdated and not specific to .NET 9.

I am upgrading my app to .NET 9 and previously used this to use commands from my page's vm in a datatemplate for listviews and such:

<ContentPage
    ...
    x:Name="this"
    x:DataType="viewmodel:MyViewModel">
    ...

    <DataTemplate x:DataType="model:MyClass">
        <Button
            Command="{Binding Source={x:Reference this}, Path=BindingContext.MyCommand}}"
            CommandParameter="{Binding .}" />
            ...

But now it seems due to the changes to compiled bindings, this no longer works. Does anyone know the new way of doing this? I've looked around online but all the answers are outdated and not specific to .NET 9.

Share Improve this question edited Mar 30 at 1:59 Ken White 126k15 gold badges236 silver badges466 bronze badges asked Mar 30 at 1:33 codybchaplincodybchaplin 1991 silver badge10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Well I ended up figuring it out soon after posting this thanks to this thread. I ended up changing:

Command="{Binding Source={x:Reference this}, Path=BindingContext.MyCommand}}"

to

Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MyViewModel}}, Path=MyCommand, x:DataType=viewmodel:MyViewModel}"

发布评论

评论列表(0)

  1. 暂无评论