英文:
How to track changes of a property of a model
问题
我有一个模型上的这些属性。
public class Student : BindableBase
{
public Guid Id { get; set; }
public string Fullname { get; set; }
private bool _isSelected;
public bool IsSelected { get => _isSelected; set => SetProperty(ref _isSelected, value); }
}
在我的ViewModel中,基本上加载所有的学生并将其分配到集合中。
public ObservableRangeCollection<Student> Students { get; } = new();
private List<Guid> SelectedIds { get; set; }
public override async Task OnActivatedAsync()
{
var results = await _service.GetAllStudents(take: 100);
Students.ReplaceRange(results);
}
在我的XAML中,
<CollectionView ItemsSource="{x:Binding Students}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="vm:Student">
<StackLayout>
<StackLayout Margin="0,20" Orientation="Horizontal">
<CheckBox IsChecked="{x:Binding IsSelected}" />
<StackLayout>
<Label
FontAttributes="Bold"
FontSize="17"
Text="{x:Binding Fullname}"
TextColor="{x:StaticResource ColorBlack}"
VerticalOptions="Center" />
</StackLayout>
</StackLayout>
<BoxView HeightRequest="1" Color="Gray" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
现在我想要实现的是,每当我选择一个项目时,我希望它立即添加到一个新的List<Guid>
对象中。但目前我不知道如何做到这一点。
任何帮助都会非常感激。
提前致谢。
我可以使用Linq来检查哪些学生的IsSelected
属性为true,但这是在保存按钮时进行的。但是我现在想做的是在选中和取消选中时将其添加/移除到一个新的List<Guid>
对象中,我在如何实现这个过程中遇到了困难。
英文:
I have this properties on my model.
public class Student : BindableBase
{
public Guid Id { get; set; }
public string Fullname { get; set; }
private bool _isSelected;
public bool IsSelected { get => _isSelected; set => SetProperty(ref _isSelected, value); }
}
And in my ViewModel basically loads all the students and assigned it into the Collection.
public ObservableRangeCollection<Student> Students { get; } = new();
private List<Guid> SelectedIds { get; set; }
public override async Task OnActivatedAsync()
{
var results = await _service.GetAllStudents(take: 100);
Students.ReplaceRange(results);
}
And in my Xaml
<CollectionView ItemsSource="{x:Binding Students}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="vm:Student">
<StackLayout>
<StackLayout Margin="0,20" Orientation="Horizontal">
<CheckBox IsChecked="{x:Binding IsSelected}" />
<StackLayout>
<Label
FontAttributes="Bold"
FontSize="17"
Text="{x:Binding Fullname}"
TextColor="{x:StaticResource ColorBlack}"
VerticalOptions="Center" />
</StackLayout>
</StackLayout>
<BoxView HeightRequest="1" Color="Gray" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Now what I want to achieve is that whenever I select an item I want it to be added to a new List of items object right away. But for now I don't have an idea how to do such thing.
Any help is much appreciated.
Thanks in advance
I can check who among of the students that has IsSelected = true by using Linq but this is during Save button. But what I wanted to do now is during check and uncheck it is being added/remove in a new List<Guid> object, which is I'm having a hard time how to implement.
答案1
得分: 1
你必须将CollectionView
的SelectedItem
属性绑定到模型中的属性:
<CollectionView ItemsSource="{Binding View}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
以及模型中的属性:
public Student SelectedItem
{
get
{
return selectedItem;
}
set
{
selectedItem = value;
DoSomeActionWithSelectedItem(selectedItem);
}
}
private Student selectedItem;
private void DoSomeActionWithSelectedItem(Student student)
{
// 在函数 'DoSomeActionWithSelectedItem' 中,你可以将学生添加到列表等等。
// .....
}
英文:
You must binding property 'SelectedItem' of CollectionView to property in model:
<CollectionView ItemsSource="{Binding View}" SelectedItem="{Binding> SelectedItem, Mode=TwoWay}">
And property in model:
public Student SelectedItem
{
get
{
return selectedItem;
}
set
{
selectedItem = value;
DoSomeActionWithSelectedItem(selectedItem);
}
}
private Student selectedItem;
private void DoSomeActionWithSelectedItem(Student student)
{
.....
}
In function 'DoSomeActionWithSelectedItem' you can add to list etc.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论