admin管理员组

文章数量:1356097

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}"

本文标签: mvvmHow to access command from vm from within a datatemplateStack Overflow