хочу вывести данные в 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>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem
Text="Edit"
BackgroundColor="LightYellow"/>
<SwipeItem
Text="Delete"
BackgroundColor="LightPink"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:ToDoViewModel}}, Path=RemoveCommand}"
CommandParameter="{Binding .}"/>
</SwipeItems>
</SwipeView.RightItems>
<Frame CornerRadius="0">
<VerticalStackLayout Spacing="5">
<Label
Text="{Binding Title}"
FontSize="24"
FontAttributes="Bold"/>
<Grid VerticalOptions="CenterAndExpand">
<Label
HorizontalOptions="StartAndExpand"
LineBreakMode="TailTruncation"
FontSize="16"
Text="{Binding Text}"/>
<Label
Text="{Binding CreateDate, StringFormat='{0:dd.MM.yyyy}'}"
HorizontalOptions="EndAndExpand"
VerticalOptions="EndAndExpand"/>
</Grid>
</VerticalStackLayout>
</Frame>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
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<Task>();
}
[RelayCommand]
void Add()
{
if (string.IsNullOrEmpty(text))
return;
Task task = new Task
{
Title = $"Task #{count}",
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;
}
}
CollectionViewSource. – EvgeniyZ Nov 12 '22 at 09:26