如何在xaml文件中访问ObservableCollection<Object>的字段

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

我正在尝试在 MAUI xaml 文件上打印我的列表的标题,但标题没有出现。我想访问标题字段和其中的其他字段。我正在使用它们打印待办事项列表。

这是我的 xaml 文件:

            <CollectionView 
                ItemsSource="{Binding Items}">

                <CollectionView.ItemTemplate>
                    <DataTemplate x:DataType="model:ToDoItem">

                        <Grid Padding="10"
                              ColumnSpacing="10" 
                              RowSpacing="10"
                              ColumnDefinitions=".60*,.20*,.20*" >

                            <Frame BorderColor="#EF7C8E" CornerRadius="5">
                                <Label 
                                    Grid.Column="0"
                                    TextColor="Black"
                                    FontSize="10"
                                    Text="{Binding Title}"/>
                            </Frame>
                        
                            <Button 
                                Grid.Column="1"
                                Text="Done" 
                                TextColor="Black"
                                FontSize="10"
                                Command="{Binding UpdateCommand}"
                                CommandParameter="{Binding Id}"/>
                            <Button 
                                Grid.Column="2"
                                Text="Delete" 
                                TextColor="Black"
                                FontSize="10"
                                Command="{Binding DeleteCommand}"
                                CommandParameter="{Binding Id}"/>
                            
                        </Grid>

                    </DataTemplate>
                </CollectionView.ItemTemplate>

            </CollectionView>

这也是我的 MainPageViewModel :

public partial class MainPageViewModel : ObservableObject
{
    [ObservableProperty]
    private Guid id;

    [ObservableProperty]
    private bool isDone;

    [ObservableProperty]
    private string title = string.Empty;

    [ObservableProperty]
    private int? priority;

    [ObservableProperty]
    ObservableCollection<ToDoItem> items = [];
}

我的 ToDoItem 类有 Guid id,字符串标题:

public class ToDoItem(string title, int priority)
 {
     public Guid Id = Guid.NewGuid();
     public DateTime CreationTime = DateTime.Now;
     public bool IsDone = false;
     public string Title = title;
     public int Priority = priority;
 }
c# .net data-binding maui
1个回答
0
投票

正如评论中已经提到的那样,您只能绑定到公共属性

public partial class MainPageViewModel : ObservableObject
{
    [ObservableProperty]
    public Guid id;

    [ObservableProperty]
    public bool isDone;

    [ObservableProperty]
    public string title = string.Empty;

    [ObservableProperty]
    public int? priority;

    [ObservableProperty]
    public ObservableCollection<ToDoItem> items = [];
}
© www.soinside.com 2019 - 2024. All rights reserved.