通过搜索词前面的单词对WPF数据网格进行排序

时间:2014-02-14 05:03:07

标签: c# wpf sorting datagrid

我有一个数据网格,我正在尝试排序。 datagrid有一列字符串。在此列中,我通过搜索字符串执行过滤。一旦完成,我想通过前面的单词或单元格中搜索字符串后面的单词对数据网格进行排序。我将如何进行这样的排序?

更新

这是我到目前为止所得到的。

private void tbSearch_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox tb = sender as Textbox;
        _searchstr = tb.Text;
        ICollectionView view = CollectionViewSource.GetDefaultView(SentenceLists);
        view.Filter = new Predicate<object>(FilterBySearch);
    }

这是过滤器函数,我想在我的SentenceLists对象中添加另一个属性sortSord并对其进行排序。但是我无法让它发挥作用。

private bool FilterBySearch(object item)
    {
        ServiceTemplateController.SentenceList sl = item as ServiceTemplateController.SentenceList;
        if (_searchstr.ToUpper() == "") return true;
        if (sl.sentence.Contains(_searchstr.ToUpper()))
        {
            string[] strArray = sl.sentence.Split(' ');
            for (int i = 0 ; i < strArray.Count(); i++)
            {
                if (strArray[i].Contains(_searchstr.ToUpper()))
                {
                    if (radioBefore.IsChecked == true)
                    {
                        if (i == 0) sl.sortWord = strArray[i];
                        else sl.sortWord = strArray[i - 1];
                    }
                    else  //radio after is selected then.
                    {
                        if (i == strArray.Count() - 1) sl.sortWord = strArray[i];
                        else sl.sortWord = strArray[i + 1];
                    }
                }
            }
            return true;
        }
        return false;
    }

1 个答案:

答案 0 :(得分:0)

结束很容易。刚刚为绑定到sortWord的数据网格添加了另一个隐藏列。然后,我将使用ICollectionView sortdescriptions按此列排序。 代码

private void tbSearchRun()
    {
        TextBox textbox = tbSearch as TextBox;
        if (textbox != null)
        {
            _searchstr = textbox.Text;
            if (_searchstr == null) return;
            ICollectionView view = CollectionViewSource.GetDefaultView(SentenceLists);
            view.Filter = new Predicate<object>(FilterBySearch);
            view.SortDescriptions.Clear();
            view.SortDescriptions.Add(new SortDescription("sortWord", ListSortDirection.Ascending));
            view.Refresh();
        }
    }
private bool FilterBySearch(object item)
    {
        ServiceTemplateController.SentenceList sl = item as ServiceTemplateController.SentenceList;
        if (_searchstr.ToUpper() == "") return true;
        if (sl.sentence.Contains(_searchstr.ToUpper()))
        {
            string[] strArray = sl.sentence.Split(' ');
            for (int i = 0; i < strArray.Count(); i++)
            {
                if (strArray[i].Contains(_searchstr.ToUpper()))
                {
                    if (radioBefore.IsChecked == true)
                    {
                        if (i == 0) sl.sortWord = strArray[i];
                        else sl.sortWord = strArray[i - 1];
                    }
                    else  //radio after is selected then.
                    {
                        if (i == strArray.Count() - 1) sl.sortWord = strArray[i];
                        else sl.sortWord = strArray[i + 1];
                    }
                    break;
                }
            }
            return true;
        }
        return false;
    }