2015-12-23 3 views
0

J'essaie de naviguer vers un élément spécifique dans ListCollectionView en fonction de la valeur de la propriété Date de SelectedDay.Accédez à un élément spécifique dans ListCollectionView

VM

private Day _selectedDay; 
public Day SelectedDay // the Name property 
{ 
    get { return _selectedDay; } 
    set { _selectedDay = value; RaisePropertyChanged(); } 
} 

public ObservableCollection<ShootingDay> AllShootingDayInfo {get; set;} 

private ListCollectionView _shootingDayInfoList; 
public ListCollectionView ShootingDayInfoList 
{ 
    get 
    { 
     if (_shootingDayInfoList == null) 
     { 
      _shootingDayInfoList = new ListCollectionView(AllShootingDayInfo);} 
      return _shootingDayInfoList; 
    } 
    set 
    { 
     _shootingDayInfoList = value; RaisePropertyChanged(); 
    } 
} 

L'objet <Day> a une propriété de Date et je veux que pour correspondre à la Date propriété dans le <ShootingDay> objet pour que je puisse accéder à l'élément dans les ShootingDayInfoListSelectedDay.Date matchs Date de l'article à l'intérieur ShootingDayInfoList.

J'ai essayé mais cela ne fonctionne pas car l'élément sélectionné ne fait pas partie du même objet.

ShootingDayInfoList.MoveCurrentTo(SelectedDay.Date); 

Comment est-ce que je peux faire ce travail? Je suis très nouveau à tout cela.

+0

Avez-vous essayé de trouver l'objet ShootingDay dans le premier ShootingDayInfoList? puis utilisez l'objet trouvé pour remplacer ce paramètre 'SelectedDay.Date'. Vous pouvez essayer de le trouver en utilisant la boucle manuelle ou Linq – dnr3

Répondre

1

Vous avez besoin du prédicat Filter pour obtenir l'élément requis, puis supprimez ce Filter pour ramener tous les articles.

code

ViewModel vm = new ViewModel(); 
System.Diagnostics.Debug.WriteLine(vm.ShootingDayInfoList.Count.ToString()); 
vm.SelectedDay.Date = DateTime.Parse("12/25/2015"); 

vm.ShootingDayInfoList.Filter = (o) => 
{ 
    if (((ShootingDay)o).Date.Equals(vm.SelectedDay.Date)) 
     return true; 

    return false; 
}; 

ShootingDay foundItem = (ShootingDay)vm.ShootingDayInfoList.GetItemAt(0); 
vm.ShootingDayInfoList.Filter = (o) => { return true; }; 

vm.ShootingDayInfoList.MoveCurrentTo(foundItem); 

J'ai vérifié le code en utilisant MoveCurrentToNext() method, il fonctionne correctement. Cette approche n'affectera pas votre code existant.

2ème approche, utiliser AllShootingDayInfo directement ou utiliser SourceCollection propriété pour obtenir sous-jacente Collection:

ViewModel vm = new ViewModel(); 
    System.Diagnostics.Debug.WriteLine(vm.ShootingDayInfoList.Count.ToString()); 
    vm.SelectedDay.Date = DateTime.Parse("12/23/2015"); 

    IEnumerable<ShootingDay> underlyingCollection = ((IEnumerable<ShootingDay>)vm.ShootingDayInfoList.SourceCollection); 

    ShootingDay d1 = underlyingCollection.FirstOrDefault(dt => dt.Date.Equals(vm.SelectedDay.Date)); 

    vm.ShootingDayInfoList.MoveCurrentTo(d1);