You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI中在ListView的ItemTemplate内使用TemplatedView异常问题

问题原因

在ListView或CollectionView的ItemTemplate中使用TemplatedView时,ControlTemplate内部的默认绑定上下文是TemplatedView本身,而非列表项对应的数据对象。直接用{Binding BookName}会因找不到目标属性导致内容无法显示,但列表项的布局占位仍存在,因此仍能触发选中操作。

解决方案

修改ControlTemplate内的绑定逻辑,让绑定指向列表项的数据对象即可。有两种简洁的实现方式:

方式一:通过RelativeSource绑定到TemplatedView的BindingContext

<ListView x:Name="listView"
          ItemsSource="{Binding BookInfo}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <TemplatedView>
                        <TemplatedView.ControlTemplate>
                            <ControlTemplate>
                                <Grid Padding="10">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="0.4*" />
                                        <RowDefinition Height="0.6*" />
                                    </Grid.RowDefinitions>
                                    <Label Text="{Binding BindingContext.BookName, Source={RelativeSource TemplatedParent}}"
                                           FontAttributes="Bold"
                                           TextColor="Teal"
                                           FontSize="21" />
                                    <Label Grid.Row="1"
                                           Text="{Binding BindingContext.BookDescription, Source={RelativeSource TemplatedParent}}"
                                           TextColor="Teal"
                                           FontSize="15" />
                                </Grid>
                            </ControlTemplate>
                        </TemplatedView.ControlTemplate>
                    </TemplatedView>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

方式二:直接给TemplatedView绑定列表项上下文

<ListView x:Name="listView"
          ItemsSource="{Binding BookInfo}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.View>
                    <!-- 显式将TemplatedView的上下文绑定到列表项 -->
                    <TemplatedView BindingContext="{Binding}">
                        <TemplatedView.ControlTemplate>
                            <ControlTemplate>
                                <Grid Padding="10">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="0.4*" />
                                        <RowDefinition Height="0.6*" />
                                    </Grid.RowDefinitions>
                                    <Label Text="{Binding BookName}"
                                           FontAttributes="Bold"
                                           TextColor="Teal"
                                           FontSize="21" />
                                    <Label Grid.Row="1"
                                           Text="{Binding BookDescription}"
                                           TextColor="Teal"
                                           FontSize="15" />
                                </Grid>
                            </ControlTemplate>
                        </TemplatedView.ControlTemplate>
                    </TemplatedView>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

内容的提问来源于stack exchange,提问作者Riyas hameed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 22:15:13