无法让最基本的数据绑定与ListView中的<DataTemplate><ViewCell><local:MyCustomView Param={Binding .}>一起使用,但项目会加载

问题描述 投票:0回答:1

描述

这是我得到的结果:

KCellSetupPage.xaml

    <ContentPage.BindingContext>
        <vm:KCellSetupViewModel />
    </ContentPage.BindingContext>

    <ContentPage.Behaviors>
        <toolkit:EventToCommandBehavior
           EventName="Appearing"
           Command="{Binding GetContentCommand}" />
    </ContentPage.Behaviors>

    <ContentPage.Content>
        <Grid>
            <RefreshView Command="{Binding GetContentCommand}"
                    IsRefreshing="{Binding IsBusy}">

                <ListView x:Name="CommandListView"
                            ItemsSource="{Binding Commands}"
                            SelectedItem="{Binding SelectedCommand}"
                            SelectionMode="Single">

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <local:ModbusCommandView Command="{Binding .}" x:DataType="models:ModbusCommand" />
                            </ViewCell>
                        </DataTemplate>             
                        
                    </ListView.ItemTemplate>
                    
                </ListView>
            </RefreshView>

所以它看起来很蹩脚,默认样式 - 通常的 ListView、胖边框发生了什么?

所以我认为这是一个绑定问题,因为它带有这些:

Severity    Count   Data Context    Binding Path    Target  Target Type Description File    Line    Project
Error   2   KCellSetupViewModel .   ModbusCommandView.Command   ModbusCommand   'MyProject.ViewModels.KCellSetupViewModel' cannot be converted to type 'MyProject.Models.ModbusCommand' C:\Users\fruit\OneDrive\Desktop\Code\[....]\Views\KCellSetupPage.xaml   38  MyProject

问题

所以我的问题是,为什么我不能自然地使用 {Binding .} 单独绑定到 ListView 的 ItemSource 中的 ModbusCommand 对象?下一个问题是,我应该放什么来代替 {Binding .}?

谢谢。

我尝试过的事情

{Binding Source={Relative Source .}}
- 期待修复某些问题,但错误本质上是同一件事 - 绑定错误。
{Binding Source={x:Reference .}}
——我现在不知道我在这里做什么,并且没有关于这个主题的好的参考。

我只是想到了一个 hackfix:因为我可以成功绑定到项目的(ModbusCommand 类型)成员,例如描述(如图所示),那么也许我可以放置一个名为“This”的 ModbusCommand 类型的公共属性,然后它返回 C# 中的 this 引用。

请引导我进入 .NET MAUI 数据绑定世界! 🌎

c# .net xaml data-binding maui
1个回答
0
投票

绑定已损坏。

在 MAUI 存储库中检查我的错误报告。我在那里发布了解决我的问题的方法。可能会有帮助。

https://github.com/dotnet/maui/issues/12035

© www.soinside.com 2019 - 2024. All rights reserved.