如何跟踪模型属性的更改

huangapple go评论91阅读模式
英文:

How to track changes of a property of a model

问题

我有一个模型上的这些属性。

  1. public class Student : BindableBase
  2. {
  3. public Guid Id { get; set; }
  4. public string Fullname { get; set; }
  5. private bool _isSelected;
  6. public bool IsSelected { get => _isSelected; set => SetProperty(ref _isSelected, value); }
  7. }

在我的ViewModel中,基本上加载所有的学生并将其分配到集合中。

  1. public ObservableRangeCollection<Student> Students { get; } = new();
  2. private List<Guid> SelectedIds { get; set; }
  3. public override async Task OnActivatedAsync()
  4. {
  5. var results = await _service.GetAllStudents(take: 100);
  6. Students.ReplaceRange(results);
  7. }

在我的XAML中,

  1. <CollectionView ItemsSource="{x:Binding Students}">
  2. <CollectionView.ItemTemplate>
  3. <DataTemplate x:DataType="vm:Student">
  4. <StackLayout>
  5. <StackLayout Margin="0,20" Orientation="Horizontal">
  6. <CheckBox IsChecked="{x:Binding IsSelected}" />
  7. <StackLayout>
  8. <Label
  9. FontAttributes="Bold"
  10. FontSize="17"
  11. Text="{x:Binding Fullname}"
  12. TextColor="{x:StaticResource ColorBlack}"
  13. VerticalOptions="Center" />
  14. </StackLayout>
  15. </StackLayout>
  16. <BoxView HeightRequest="1" Color="Gray" />
  17. </StackLayout>
  18. </DataTemplate>
  19. </CollectionView.ItemTemplate>
  20. </CollectionView>

现在我想要实现的是,每当我选择一个项目时,我希望它立即添加到一个新的List<Guid>对象中。但目前我不知道如何做到这一点。

任何帮助都会非常感激。

提前致谢。

我可以使用Linq来检查哪些学生的IsSelected属性为true,但这是在保存按钮时进行的。但是我现在想做的是在选中和取消选中时将其添加/移除到一个新的List<Guid>对象中,我在如何实现这个过程中遇到了困难。

英文:

I have this properties on my model.

  1. public class Student : BindableBase
  2. {
  3. public Guid Id { get; set; }
  4. public string Fullname { get; set; }
  5. private bool _isSelected;
  6. public bool IsSelected { get =&gt; _isSelected; set =&gt; SetProperty(ref _isSelected, value); }
  7. }

And in my ViewModel basically loads all the students and assigned it into the Collection.

  1. public ObservableRangeCollection&lt;Student&gt; Students { get; } = new();
  2. private List&lt;Guid&gt; SelectedIds { get; set; }
  3. public override async Task OnActivatedAsync()
  4. {
  5. var results = await _service.GetAllStudents(take: 100);
  6. Students.ReplaceRange(results);
  7. }

And in my Xaml

  1. &lt;CollectionView ItemsSource=&quot;{x:Binding Students}&quot;&gt;
  2. &lt;CollectionView.ItemTemplate&gt;
  3. &lt;DataTemplate x:DataType=&quot;vm:Student&quot;&gt;
  4. &lt;StackLayout&gt;
  5. &lt;StackLayout Margin=&quot;0,20&quot; Orientation=&quot;Horizontal&quot;&gt;
  6. &lt;CheckBox IsChecked=&quot;{x:Binding IsSelected}&quot; /&gt;
  7. &lt;StackLayout&gt;
  8. &lt;Label
  9. FontAttributes=&quot;Bold&quot;
  10. FontSize=&quot;17&quot;
  11. Text=&quot;{x:Binding Fullname}&quot;
  12. TextColor=&quot;{x:StaticResource ColorBlack}&quot;
  13. VerticalOptions=&quot;Center&quot; /&gt;
  14. &lt;/StackLayout&gt;
  15. &lt;/StackLayout&gt;
  16. &lt;BoxView HeightRequest=&quot;1&quot; Color=&quot;Gray&quot; /&gt;
  17. &lt;/StackLayout&gt;
  18. &lt;/DataTemplate&gt;
  19. &lt;/CollectionView.ItemTemplate&gt;
  20. &lt;/CollectionView&gt;

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

你必须将CollectionViewSelectedItem属性绑定到模型中的属性:

  1. <CollectionView ItemsSource="{Binding View}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">

以及模型中的属性:

  1. public Student SelectedItem
  2. {
  3. get
  4. {
  5. return selectedItem;
  6. }
  7. set
  8. {
  9. selectedItem = value;
  10. DoSomeActionWithSelectedItem(selectedItem);
  11. }
  12. }
  13. private Student selectedItem;
  14. private void DoSomeActionWithSelectedItem(Student student)
  15. {
  16. // 在函数 'DoSomeActionWithSelectedItem' 中,你可以将学生添加到列表等等。
  17. // .....
  18. }
英文:

You must binding property 'SelectedItem' of CollectionView to property in model:

  1. &lt;CollectionView ItemsSource=&quot;{Binding View}&quot; SelectedItem=&quot;{Binding&gt; SelectedItem, Mode=TwoWay}&quot;&gt;

And property in model:

  1. public Student SelectedItem
  2. {
  3. get
  4. {
  5. return selectedItem;
  6. }
  7. set
  8. {
  9. selectedItem = value;
  10. DoSomeActionWithSelectedItem(selectedItem);
  11. }
  12. }
  13. private Student selectedItem;
  14. private void DoSomeActionWithSelectedItem(Student student)
  15. {
  16. .....
  17. }

In function 'DoSomeActionWithSelectedItem' you can add to list etc.

huangapple
  • 本文由 发表于 2023年2月8日 20:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定