这是我得到的结果:
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 数据绑定世界! 🌎