dimanche 4 juillet 2021

How to bind to specific elements of a collection?

For example, I have a collection where data is stored like this:

class MyVector3
{
   double X { get; set; }
   double Y { get; set; }
   double Z { get; set; }
}
ObservableCollection<MyVector3> Positions { get; } = new ObservableCollection<MyVector3>();
Positions.Add(new MyVector3() { X = 0.0, Y = 0.0, Z = 0.0 });
Positions.Add(new MyVector3() { X = 1.0, Y = 1.0, Z = 1.0 });
Positions.Add(new MyVector3() { X = 2.0, Y = 2.0, Z = 2.0 });
...

Represent in a image, it is as follows.

Image1

And I want to bind the value of a specific element in the Positions property.

class ModelData
{
   List<MyVector3> ModelPositions { get; set; }
   ...
}
SomeCollection<ModelData> ModelDatas { get; } = new SomeCollection<ModelData>();
ModelDatas.Add(new ModelData() // ModelData 1
{
   ModelPositions = new List<MyVector3>()
   {
      Positions[0],
      Positions[3],
      Positions[9],
      ...
   };
});
ModelDatas.Add(new ModelData() // ModelData 2
{
   ModelPositions = new List<MyVector3>()
   {
      Positions[4],
      Positions[3],
      Positions[7],
      Positions[10],
      ...
   };
});
...

The state of the Positions property is always changing. Elements can be added or deleted, and the element's data can also change. And all these changes should be reflected to the ModelData object.

In the sample code above, the ModelData 1 and ModelData 2 share the value of index 3 of the Postions property. If the value of the index 3 element of the Positions property is changed, how to notify this change only to the elements of ModelData 1 and ModelData 2 that added to the ModelDatas property?

Aucun commentaire:

Enregistrer un commentaire