简体   繁体   English

.NET MAUI Communitytoolkit.Mvvm:如何绑定 object 而不是字符串

[英].NET MAUI Communitytoolkit.Mvvm: How to bind an object instead of a string

i already the project from the official tutorial of .NET MAUI until step 5 using Communitytoolkit.Mvvm: text我已经从 .NET MAUI 的官方教程到第 5 步使用 Communitytoolkit.Mvvm 的项目: 文本

Now, instead of binding only a Text (which is a standard type that can be accessed from everywhere) i would like to bind a simple object (called ItemGroup) with two members (bool isChecked and string name).现在,我不想只绑定一个文本(这是一种可以从任何地方访问的标准类型),而是想绑定一个简单的 object(称为 ItemGroup)和两个成员(bool isChecked 和字符串名称)。

How to do that?怎么做? For a global access i made this class in the MainView folder called ItemGroup.对于全局访问,我在名为 ItemGroup 的 MainView 文件夹中创建了这个 class。 This class is not accessable and i don't know how to do that it is.这个 class 不可访问,我不知道该怎么做。 I changed the code in the MainPage.xaml like this:我像这样更改了 MainPage.xaml 中的代码:

                <CollectionView ItemsSource="{Binding Items}"
                                Grid.Row="1">
                    <CollectionView.ItemTemplate>
                        <DataTemplate x:DataType="{x:Type x:ViewModel.ItemGroup}">
                            <Grid >
                                <CheckBox IsChecked="{Binding ItemGroup.IsChecked}" Grid.Column="0"/>
                                <Label Text="{Binding ItemGroup.name}"  Padding="10" Grid.Column="1"                                                                  
                                    BackgroundColor="LightGray"/>
                            </Grid>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

See also the project structure with the ItemGroup class in the ViewModel folder as well as the error message:另请参阅 ViewModel 文件夹中项目组 class 的项目结构以及错误消息:

在此处输入图像描述

where the content page is declared as this:内容页面声明如下:

在此处输入图像描述

Remark: The MainViewModel looks like this:备注:MainViewModel 看起来像这样:

在此处输入图像描述

  • Should i declare some uses, or namespace?我应该声明一些用途或命名空间吗?
  • where in the project should i place the Class of the objects i would bind?我应该在项目的哪个位置放置我要绑定的对象的 Class?

Thanks in advance, Thomas提前致谢, 托马斯

Also tryed to implement the class ItemGroup in the MainViewModel.cs but then i had any more problems with access to this class.还尝试在 MainViewModel.cs 中实现 class ItemGroup,但随后我在访问此 class 时遇到了更多问题。

Try this尝试这个

<Label Text="{Binding name}" …

Note that name must be a public property注意name必须是公共属性

I believe your problem lies with you Binding declaration inside you ItemTemplate.我相信你的问题在于你在 ItemTemplate 中的 Binding 声明。 Given that you bound your CollectionView's ItemSource to "items", the ItemTemplate's data context is now a single itemGroup from your list.鉴于您将 CollectionView 的 ItemSource 绑定到“items”,ItemTemplate 的数据上下文现在是列表中的单个 itemGroup。 Therefore, you should not be writing因此,你不应该写

<CheckBox IsChecked="{Binding ItemGroup.IsChecked}"[...]

But instead但反而

<CheckBox IsChecked="{Binding isChecked}"[...]

Since you are already "within" a single itemgroup object. Same goes for your Label. Furthermore, I don't think you actually need to declare the Datatype for your item's DataTemplate.由于您已经在单个项目组 object 中。您的 Label 也是如此。此外,我认为您实际上不需要为项目的数据模板声明数据类型。

ps Watch out for your item declaration, it seems like you have both items and Items declared in your ViewModel; ps 注意你的 item 声明,看起来你的 ViewModel 中同时声明了 items 和 Items; and both seem public.两者似乎都是公开的。

You have a number of issues here.你在这里有很多问题。

The community mvvm toolkit includes code generators.社区 mvvm 工具包包括代码生成器。

A variable:一个变量:

[ObservableProperty]    
string text;

Is examined by the code generator at compile time and in a partial class it will add a public property Text.在编译时由代码生成器检查,在部分 class 中,它将添加公共属性文本。

Binding is case sensitive and you need to bind to public properties so you need to use upper case on that first letter.绑定区分大小写,您需要绑定到公共属性,因此您需要在第一个字母上使用大写。 Binding Text rather than Binding text.绑定文本而不是绑定文本。

You might have other instances where you did the same sort of thing.您可能在其他情况下做过同样的事情。

I don't see where you use it but that add [Relaycommand] will generate AddCommand as a public property which you bind to.我没有看到你在哪里使用它,但是 add [Relaycommand] 将生成 AddCommand 作为你绑定的公共属性。

Additionally you have misunderstood how an itemscontrol gets an item out a bound collection per row.此外,您误解了 itemscontrol 如何从每行的绑定集合中获取项目。 As DevenCC points out, once you bind ItemsSource to Items then each row will get an instance of whatever is in that collection so you should bind IsChecked rather than ParentProperty.IsChecked.正如 DevenCC 指出的那样,一旦将 ItemsSource 绑定到 Items,那么每一行都将获得该集合中任何内容的实例,因此您应该绑定 IsChecked 而不是 ParentProperty.IsChecked。

As to where to put classes.至于在哪里上课。 In large apps it is a nuisance to flip between a folder contains views and another contains viewmodels.在大型应用程序中,在包含视图的文件夹和包含视图模型的文件夹之间切换是一件很麻烦的事情。 You might want to consider a folder contains each pair of view and viewmodel.您可能需要考虑一个包含每对视图和视图模型的文件夹。 Hence a foo folder containing fooView and fooViewModel因此,包含 fooView 和 fooViewModel 的 foo 文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示 ObservableGroupedCollection 的正确方法<string, telement>使用 Wpf .NET 6 和 CommunityToolkit.Mvvm 包</string,> - Proper way of displaying an ObservableGroupedCollection<string, TElement> using Wpf .NET 6 and the CommunityToolkit.Mvvm Package C# CommunityToolkit.Mvvm ObservableProperty 列表 - C# CommunityToolkit.Mvvm ObservableProperty on a list .Net MAUI - 如何绑定到作为 KeyValuePairs 列表的 object 中的属性 - .Net MAUI - How to bind to a property in an object that is a List of KeyValuePairs .NET MAUI - 如何绑定 ObservableCollection 项的属性 - .NET MAUI - How to bind on a property of an ObservableCollection Item 如何从 .NET MAUI 中的图像项列表中绑定图像源 - How to bind an imageSource from a List of Image items in .NET MAUI .NET Maui MVVM Picker 绑定 SelectedIndexChanged 事件 - .NET Maui MVVM Picker Binding SelectedIndexChanged Event 具有 CommunityToolkit ObservableProperty 的毛伊岛在处理时未更新 - Maui with CommunityToolkit ObservableProperty not updating while processing 如何使用 c# 而不是 xaml 创建 .Net Maui ContentPage? - How create .Net Maui ContentPage using c# instead of xaml? 如何使用 .NET MAUI 使用 Mvvm 模式通过 x:Name 获取组件? - How to get component by x:Name using Mvvm Pattern using .NET MAUI? MVVM:如何绑定到ListBox? - MVVM: How to bind to a ListBox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM