.NET MAUI在单个XAML标记上绑定两个数据类型

huangapple go评论75阅读模式
英文:

.NET MAUI binding two DataType on single XAML tag

问题

我有以下的XAML代码:

<ContentPage x:DataType="viewmodel:MonkeyViewModel">
    <CollectionView ItemsSource="{Binding MonkeyList}">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="model:Monkey">
                <Label Text="{Binding Id}" />
                <Label Text="{Binding Name}" />
                <Label Text="Edit">
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer 
                            Command="{Binding EditCommand}" 
                            CommandParameter="{Binding Id}"/>
                    </Label.GestureRecognizers>
                </Label>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentPage>

MonkeyListEditCommand 来自于 viewmodel:MonkeyViewModel

IdName 来自于 model:Monkey

在标签 <TapGestureRecognizer> 中,{Binding EditCommand} 由于在 Monkey 的数据上下文中找不到 EditCommand 而无法工作。

如何使 EditCommand 绑定到 MonkeyViewModel,而 Id 绑定到 Monkey?我期望 <Label> 的工作方式类似于 HTML 的 <A> 链接:<a href="javascript:edit(id)">Edit</a> 或:<a onclick="edit(id)">Edit</a>

英文:

I have the following XAML code:

<ContentPage x:DataType="viewmodel:MonkeyViewModel">
    <CollectionView ItemsSource="{Binding MonkeyList}">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="model:Monkey">
                <Label Text="{Binding Id}" />
                <Label Text="{Binding Name}" />
                <Label Text="Edit">
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer 
                            Command="{Binding EditCommand}" 
                            CommandParameter="{Binding Id}"/>
                    </Label.GestureRecognizers>
                </Label>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</ContentPage>

The MonkeyList and EditCommand come from viewmodel:MonkeyViewModel.

The Id and Name come from model:Monkey.

On the tag <TapGestureRecognizer> , the {Binding EditCommand} doesn't work due to the EditCommand not being found in the data context of Monkey.

How can I make the EditCommand binding on MonkeyViewModel and Id binding on Monkey?

I would expect the <Label> works like a html <A> link: <a href="javascript:edit(id)">Edit</a> or: <a onclick="edit(id)">Edit</a>

答案1

得分: 1

Change the Command binding to use a Relative Binding as follows:

<TapGestureRecognizer 
    Command="{Binding EditCommand, Source={RelativeSource AncestorType={x:Type viewmodel:MonkeyViewModel}}}" 
    CommandParameter="{Binding Id}"/>
英文:

Just change the Command binding to use a Relative Binding as follows:

<TapGestureRecognizer 
    Command="{Binding EditCommand, Source={RelativeSource AncestorType={x:Type viewmodel:MonkeyViewModel}}}" 
    CommandParameter="{Binding Id}"/>

huangapple
  • 本文由 发表于 2023年5月18日 04:26:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定