【问题标题】:Select specific record in CollectionVIewSource using code使用代码选择 CollectionVIewSource 中的特定记录
【发布时间】:2014-10-27 13:26:37
【问题描述】:

喂。我有一个明显高于我的简单问题。如何获取 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);
}   

但它所做的只是清除所有字段。知道我该怎么做吗? 不管我传入什么数字,结果总是一样的。

【问题讨论】:

  • 不确定我们是否可以将查询传递给MoveCurrentTo?但正如文档所说,传入的参数应该是代表该项目的对象。在这种情况下,您应该使用FirstOrDefault 而不是Where,然后检查select 是否不为空,然后再将其传递给MoveCurrentTo
  • @KingKing - 结果相同。如果我可以问,您将如何搜索行值?使用集合视图是否是解决此问题的正确方法?
  • “清除所有字段”是什么意思?您的意思是取消选择当前选定的行吗?
  • @MikeStrobel 是的。这正是它的作用。如果我使用的是文本框,则在单击按钮时它们都会变为空白

标签: c# wpf mvvm entity-framework-6


【解决方案1】:

我认为过滤器是您正在寻找的。这是一个使用过滤器的示例代码。

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

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2015-01-10
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多