如何跟踪模型属性的更改

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

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 =&gt; _isSelected; set =&gt; SetProperty(ref _isSelected, value); }
}

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

public ObservableRangeCollection&lt;Student&gt; Students { get; } = new();
private List&lt;Guid&gt; SelectedIds { get; set; }

public override async Task OnActivatedAsync()
{
	 var results = await _service.GetAllStudents(take: 100);
	 Students.ReplaceRange(results);
}

And in my Xaml

&lt;CollectionView ItemsSource=&quot;{x:Binding Students}&quot;&gt;
	&lt;CollectionView.ItemTemplate&gt;
		&lt;DataTemplate x:DataType=&quot;vm:Student&quot;&gt;
			&lt;StackLayout&gt;
				&lt;StackLayout Margin=&quot;0,20&quot; Orientation=&quot;Horizontal&quot;&gt;
					&lt;CheckBox IsChecked=&quot;{x:Binding IsSelected}&quot; /&gt;
					&lt;StackLayout&gt;
						&lt;Label
							FontAttributes=&quot;Bold&quot;
							FontSize=&quot;17&quot;
							Text=&quot;{x:Binding Fullname}&quot;
							TextColor=&quot;{x:StaticResource ColorBlack}&quot;
							VerticalOptions=&quot;Center&quot; /&gt;
					&lt;/StackLayout&gt;
				&lt;/StackLayout&gt;
				&lt;BoxView HeightRequest=&quot;1&quot; Color=&quot;Gray&quot; /&gt;
			&lt;/StackLayout&gt;
		&lt;/DataTemplate&gt;
	&lt;/CollectionView.ItemTemplate&gt;
&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属性绑定到模型中的属性:

<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:

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

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.

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:

确定