2014-10-27 71 views
0

Hie在CollectionVIewSource中选择具体记录。我有什么应该是一个简单的问题,显然在我之上。如何获得一个collectionViewSource来选择特定的记录?使用代码

我已经试过这样:

private object Select_CommandExecute(object param) 
{ 
    // Select * From Signups where Tag = '2'; 
    var select = context.signups.Where(s => s.tag == 2); 
    return signupsViewSource.View.MoveCurrentTo(select); 
} 

但它所做的是清除所有字段。任何想法我如何做到这一点? 无论我通过什么号码,结果都是一样的。

+0

不确定我们是否可以将***查询***传入“MoveCurrentTo”?但正如文档所说,传入的参数应该是表示项目的对象。在这种情况下,您应该使用'FirstOrDefault'而不是'Where',然后在将'select'传递到'MoveCurrentTo'之前检查'select'是否为空。 – 2014-10-27 13:32:40

+0

@KingKing - 同样的结果。如果我可以问,你会如何搜索一行值?正在使用集合视图,甚至正确的方式来解决这个问题? – Offer 2014-10-27 13:54:35

+0

当你说“清除所有字段”时,你是什么意思?你的意思是取消选择当前选定的行吗? – 2014-10-27 15:00:58

回答

0

我认为过滤器是你在找什么。以下是使用过滤器的示例代码。

IList<Employer> employers; 
ICollectionView _employerView; 
private string _filterString=string.Empty; 
public Window1() 
{ 
    InitializeComponent(); 
    employers = GetCustomers(); 
    _employerView = CollectionViewSource.GetDefaultView(employers); 
    _employerView.Filter = EmployerFilter; 
} 

public bool EmployerFilter(object item) 
{ 
    Employer employer = item as Employer; 
    return employer.Name.ToLower().StartsWith(_filterString.ToLower()); 
} 

public string FilterString 
{ 
    get { return _filterString; } 
    set{ 
     _filterString = value; 
     OnPropertyChanged("FilterString"); 
     _employerView.Refresh(); 
} 
} 

希望得到这个帮助。

+0

实施此操作。我会更新它的方式 – Offer 2014-10-27 14:25:50