You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

旧版Syncfusion WPF SfDataGrid实现搜索选择功能求助(v13.34000.0.7)

Implementing Search-and-Select for Syncfusion DataGrid v13.34000.0.7

Got it, since your Syncfusion DataGrid version 13.34000.0.7 doesn't include the built-in SearchHelper class that powers the search-select feature in newer versions, we can build a fully functional replacement manually. Here's how to replicate that core behavior step by step:

Step 1: Track Search State Variables

First, add private fields to your component/form to keep track of matches, current selection position, and the active search text:

private List<int> _matchedRowIndices = new List<int>();
private int _currentMatchIndex = -1;
private string _currentSearchText = string.Empty;

Step 2: Handle TextBox Text Changed Event

Replace the SearchHelper-driven logic with a custom handler that scans your data source for matches:

private void searchTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    var textBox = e.Source as TextBox;
    _currentSearchText = textBox.Text.ToLower().Trim();
    _matchedRowIndices.Clear();
    _currentMatchIndex = -1;

    // Reset state if search text is empty
    if (string.IsNullOrEmpty(_currentSearchText))
    {
        datagrid.SelectedItem = null;
        ClearRowHighlights();
        return;
    }

    // Scan data source for matching items
    var dataSource = datagrid.ItemsSource as IEnumerable<YourDataType>;
    if (dataSource != null)
    {
        int rowIndex = 0;
        foreach (var item in dataSource)
        {
            if (DoesItemMatchSearch(item, _currentSearchText))
            {
                _matchedRowIndices.Add(rowIndex);
            }
            rowIndex++;
        }
    }

    // Jump to the first match initially
    FindNextMatch();
}

Step 3: Define Match Logic

Create a method to check if a data item matches the search text. Customize this to include all the properties you want to search:

private bool DoesItemMatchSearch(YourDataType item, string searchText)
{
    // Replace these properties with your actual data model fields
    return item.Name.ToLower().Contains(searchText) ||
           item.Id.ToString().Contains(searchText) ||
           item.Description.ToLower().Contains(searchText);
}

Step 4: Implement "Find Next" Behavior

Add a method to cycle through matched items (just like SearchHelper.FindNext):

private void FindNextMatch()
{
    if (_matchedRowIndices.Count == 0)
    {
        datagrid.SelectedItem = null;
        ClearRowHighlights();
        return;
    }

    // Wrap around to the first match when reaching the end
    _currentMatchIndex = (_currentMatchIndex + 1) % _matchedRowIndices.Count;
    int targetRowIndex = _matchedRowIndices[_currentMatchIndex];

    // Select and scroll to the matched item
    var dataSource = datagrid.ItemsSource as IList;
    if (dataSource != null && targetRowIndex < dataSource.Count)
    {
        var selectedItem = dataSource[targetRowIndex];
        datagrid.SelectedItem = selectedItem;
        datagrid.ScrollIntoView(selectedItem);

        // Highlight the row for visibility (optional)
        HighlightMatchedRow(targetRowIndex);
    }
}

Step 5: Add Row Highlighting (Optional)

To replicate the visual feedback from SearchHelper, add methods to highlight matched rows and clear highlights:

private void HighlightMatchedRow(int rowIndex)
{
    ClearRowHighlights();
    var row = datagrid.GetRowAtRowIndex(rowIndex) as DataGridRow;
    if (row != null)
    {
        // Use a subtle highlight color (adjust as needed)
        row.Background = new SolidColorBrush(Color.FromArgb(0x33, 0x00, 0x7A, 0xCC));
    }
}

private void ClearRowHighlights()
{
    foreach (var row in datagrid.GetVisualChildren<DataGridRow>())
    {
        row.Background = null; // Reset to default style
    }
}

Step 6: Add Visual Tree Helper Extension

Since older Syncfusion versions might not have a built-in way to iterate through rows, add this extension method to your project:

using System.Windows;
using System.Windows.Media;
using System.Collections.Generic;

public static class VisualTreeHelperExtensions
{
    public static IEnumerable<T> GetVisualChildren<T>(this DependencyObject parent) where T : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            if (child is T typedChild)
            {
                yield return typedChild;
            }
            foreach (var grandChild in GetVisualChildren<T>(child))
            {
                yield return grandChild;
            }
        }
    }
}

Key Notes

  • Replace YourDataType with the actual type of items in your DataGrid's ItemsSource.
  • Adjust the DoesItemMatchSearch method to include all properties you want to search (e.g., emails, dates converted to strings).
  • To add a "Find Previous" feature, create a FindPreviousMatch method that decrements _currentMatchIndex (with wrap-around logic).
  • The highlight color can be customized to match your app's theme.

内容的提问来源于stack exchange,提问作者Biju Maharjan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:26:23