0

我正在尝试根据 SelectedDay 的 Date 属性的值导航到 ListCollectionView 中的特定项目。

虚拟机

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();
    }
}

<Day>对象有一个属性,Date我希望它与ObjectDate中的 Property匹配,<ShootingDay>以便我可以导航到Item inside的ShootingDayInfoListwhereSelectedDay.Date匹配项中的项目。DateShootingDayInfoList

我已经尝试过了,但这不起作用,因为 Selected Item 不是同一个对象的一部分。

ShootingDayInfoList.MoveCurrentTo(SelectedDay.Date);

我怎样才能使这项工作?我对这一切都很陌生。

4

1 回答 1

1

您需要Filter谓词来获取所需的项目,然后将其删除Filter以恢复所有项目。

代码

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); 

我已经通过使用检查了代码MoveCurrentToNext() method,它工作正常。这种方法不会影响您现有的代码。

第二种方法,AllShootingDayInfo直接使用或使用SourceCollection属性来获取底层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); 
于 2015-12-23T08:04:27.177 回答