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

.NET MAUI中CollectionView无法显示数据的技术求助

.NET MAUI CollectionView 绑定数据不显示的解决方案

你的CollectionView无法展示数据的核心原因是DataTemplate中的绑定路径错误:CollectionView的每个Item绑定上下文是单个Receipt对象,而非ReceiptsViewModel,但你当前绑定的是ViewModel中的Receipt_name、Receipt_description属性,这些属性不属于Receipt模型,导致绑定失效。

1. 修正DataTemplate的绑定路径

将Feed.xaml中DataTemplate内的Label绑定,改为指向Receipt模型的对应属性:

原错误代码:

<Label Text="{Binding Receipt_name}" VerticalTextAlignment="Center" LineBreakMode="WordWrap" FontAttributes="Bold"/>
<Label Text="{Binding Receipt_description}" TextColor="LightSlateGray" LineBreakMode="WordWrap"/>

修改后:

<Label Text="{Binding Name}" VerticalTextAlignment="Center" LineBreakMode="WordWrap" FontAttributes="Bold"/>
<Label Text="{Binding Description}" TextColor="LightSlateGray" LineBreakMode="WordWrap"/>

2. 添加编译时绑定检查(推荐)

为避免类似绑定路径错误,建议为DataTemplate指定x:DataType,启用编译时绑定验证:

步骤2.1 导入模型命名空间

在ContentPage根节点添加model命名空间引用:

xmlns:model="clr-namespace:Messenger.Model"

步骤2.2 为DataTemplate指定类型

修改DataTemplate节点:

<DataTemplate x:DataType="model:Receipt">

完整修改后的Feed.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Messenger.Feed"
             Title="Feed"
             xmlns:viewmodel ="clr-namespace:Messenger.ViewModel"
             xmlns:model="clr-namespace:Messenger.Model"
             x:DataType="viewmodel:ReceiptsViewModel">

    <Grid RowDefinitions="100, Auto, *"
          ColumnDefinitions=".75*, .25*">
        <Label Grid.ColumnSpan="2"
               HorizontalTextAlignment="Center"
               VerticalTextAlignment="Center"
               Text="Receipts"
               FontSize="40"
               BackgroundColor="AntiqueWhite"
               x:Name="l"/>
        <Button Text="+"
                HorizontalOptions="End"
                VerticalOptions="Center"
                BackgroundColor="Black"
                Grid.Row="0"
                Margin="15"
                Grid.ColumnSpan="2"
                Command="{Binding AddCommand}"/>
        <CollectionView Grid.Row="2" Grid.ColumnSpan="2" Margin="0,15,0,0"
                        ItemsSource="{Binding Receipts}">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="model:Receipt">
                    <Grid Padding="0,5">
                        <Frame Margin="3,0,5,3" BorderColor="Black">
                            <HorizontalStackLayout>
                                <Image MaximumHeightRequest="100" MaximumWidthRequest="100" Source="dish.jpg"/>
                                <VerticalStackLayout Margin="20,0,0,0" VerticalOptions="Center">
                                    <Label Text="{Binding Name}" VerticalTextAlignment="Center" LineBreakMode="WordWrap" FontAttributes="Bold"/>
                                    <Label Text="{Binding Description}" TextColor="LightSlateGray" LineBreakMode="WordWrap"/>
                                </VerticalStackLayout>
                            </HorizontalStackLayout>
                        </Frame>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </Grid>
</ContentPage>

额外注意事项

  • 点击Add按钮时,需确保ViewModel中的Receipt_name、Receipt_description等属性已正确赋值,否则新增的Receipt对象会包含空值或默认值
  • 可通过调试查看Receipts集合的Count属性,验证数据是否已正确添加到集合中

内容的提问来源于stack exchange,提问作者Дмитрий Витязь

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 15:02:06