0

хочу вывести данные в CollectionView, данные которые имеют state = false с использованием CommunityToolkit.Mvvm, но не совсем понимаю как это сделать.

Model

public class Task
{
    public string Title { get; set; }
    public string Text { get; set; }
    public bool State { get; set; }
    public DateTime CreateDate { get; set; }
}

View

<CollectionView ItemsSource="{Binding Tasks}">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <SwipeView Margin="0,0,0,10">
                                <SwipeView.LeftItems>
                                    <SwipeItems Mode="Execute">
                                        <SwipeItem 
                                            Text="Done"
                                            BackgroundColor="LightGreen"
                                            Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:ToDoViewModel}}, Path=StateDoneCommand}"
                                            CommandParameter="{Binding .}"/>
                                    </SwipeItems>
                                </SwipeView.LeftItems>
                            &lt;SwipeView.RightItems&gt;
                                &lt;SwipeItems&gt;
                                    &lt;SwipeItem 
                                        Text=&quot;Edit&quot;
                                        BackgroundColor=&quot;LightYellow&quot;/&gt;
                                    &lt;SwipeItem 
                                        Text=&quot;Delete&quot;
                                        BackgroundColor=&quot;LightPink&quot;
                                        Command=&quot;{Binding Source={RelativeSource AncestorType={x:Type viewmodel:ToDoViewModel}}, Path=RemoveCommand}&quot;
                                        CommandParameter=&quot;{Binding .}&quot;/&gt;
                                &lt;/SwipeItems&gt;
                            &lt;/SwipeView.RightItems&gt;

                            &lt;Frame CornerRadius=&quot;0&quot;&gt;
                                &lt;VerticalStackLayout Spacing=&quot;5&quot;&gt;
                                    &lt;Label
                                        Text=&quot;{Binding Title}&quot;
                                        FontSize=&quot;24&quot;
                                        FontAttributes=&quot;Bold&quot;/&gt;
                                    &lt;Grid VerticalOptions=&quot;CenterAndExpand&quot;&gt;
                                        &lt;Label
                                            HorizontalOptions=&quot;StartAndExpand&quot;
                                            LineBreakMode=&quot;TailTruncation&quot;
                                            FontSize=&quot;16&quot;
                                            Text=&quot;{Binding Text}&quot;/&gt;
                                        &lt;Label
                                            Text=&quot;{Binding CreateDate, StringFormat='{0:dd.MM.yyyy}'}&quot;
                                            HorizontalOptions=&quot;EndAndExpand&quot;
                                            VerticalOptions=&quot;EndAndExpand&quot;/&gt;
                                    &lt;/Grid&gt;
                                &lt;/VerticalStackLayout&gt;
                            &lt;/Frame&gt;

                        &lt;/SwipeView&gt;
                    &lt;/DataTemplate&gt;
                &lt;/CollectionView.ItemTemplate&gt;
            &lt;/CollectionView&gt;

ViewModel

public partial class ToDoViewModel : ObservableObject
    {
        [ObservableProperty]
        string title;
        [ObservableProperty]
        string text;
        [ObservableProperty]
        bool state = false;
        [ObservableProperty]
        DateTime createDate = DateTime.Now.Date;
        [ObservableProperty]
        ObservableCollection<Task> tasks;
    int count = 1;
    public ToDoViewModel()
    {
        tasks = new ObservableCollection&lt;Task&gt;();
    }

    [RelayCommand]
    void Add()
    {
        if (string.IsNullOrEmpty(text))
            return;
        Task task = new Task
        {
            Title = $&quot;Task #{count}&quot;,
            Text = text,
            State = state,
            CreateDate = createDate
        };
        tasks.Add(task);
        count++;
    }

    [RelayCommand]
    void Remove(Task task)
    {
        if (tasks.Contains(task))
            tasks.Remove(task);
    }

    [RelayCommand]
    void StateDone(Task task)
    {
        task.State = true;
    }
}

0 Answers0