【问题标题】:C# Searchbox in a WPF Application with a DataGrid带有 DataGrid 的 WPF 应用程序中的 C# 搜索框
【发布时间】:2014-10-24 15:28:19
【问题描述】:

您好,我在网站上进行了搜索,但并没有完全找到我的问题的答案。我读了这篇文章:http://davidowens.wordpress.com/2009/02/18/wpf-search-text-box/ 这是有人提出的关于 wpf 搜索框的另一个问题的建议。 我有一个使用 Access DB 的 C# WPF 应用程序。在我的数据输入屏幕上,我有一个搜索框和一个显示数据库中所有记录的数据网格。我目前的搜索工作但不是我想象的那样。我希望用户能够开始在搜索框中输入内容,并且数据网格中显示的记录列表将开始根据他们输入的内容进行过滤。我写的代码我认为会这样做,但为了让他们进行搜索,他们必须例如键入: people* 然后按 Enter 键,它将显示结果。我想知道有没有办法修改我的代码以不需要星号并在输入时进行过滤,还是应该以不同的方式编写? 我的数据输入 xaml 页面文本框名称如下:

<TextBox x:Name="txtSearch" Grid.Column="1" FontSize="14" Margin="10, 0, 10, 0" Text="{Binding Path=SearchString,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

在我的datahelper.cs下面:

    private void Search(string inputSearchString)
    {
        inputSearchString = inputSearchString.ToLower();

        LastSearchTerm = inputSearchString;

        FilteredCaseCollection.Clear();

        if (String.IsNullOrEmpty(inputSearchString.Trim()))
        {
            foreach (CaseViewModel caseVM in CaseCollection)
            {
                FilteredCaseCollection.Add(caseVM);
            }
        }
        else
        {
            inputSearchString = inputSearchString.Replace(" =", "=").Replace("= ", "=").Replace(" = ", "=");

            string[] termsArray = inputSearchString.Split(' ');

            int count = 0;

            foreach (CaseViewModel caseVM in CaseCollection)
            {
                count++;
                Type t = caseVM.GetType();
                foreach (PropertyInfo propertyInfo in t.GetProperties())
                {
                    if (propertyInfo.CanRead)
                    {
                        object value = propertyInfo.GetValue(caseVM, null);

                        if (value == null)
                        {
                            value = String.Empty;
                        }

                        string name = propertyInfo.Name.ToLower();

                        foreach (string term in termsArray)
                        {
                            if (term.ToLower().Equals(value.ToString().ToLower()))
                            {
                                if (!FilteredCaseCollection.Contains(caseVM))
                                {
                                    FilteredCaseCollection.Add(caseVM);
                                }
                            }
                            else if (term.ToLower().Contains("*") && MatchWildcardString(term.ToLower(), value.ToString().ToLower()))
                            {
                                if (!FilteredCaseCollection.Contains(caseVM))
                                {
                                    FilteredCaseCollection.Add(caseVM);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public bool MatchWildcardString(string pattern, string input)
    {
        if (String.Compare(pattern, input) == 0)
        {
            return true;
        }
        else if (String.IsNullOrEmpty(input))
        {
            if (String.IsNullOrEmpty(pattern.Trim(new Char[1] { '*' })))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (pattern.Length == 0)
        {
            return false;
        }
        else if (pattern[0] == '?')
        {
            return MatchWildcardString(pattern.Substring(1), input.Substring(1));
        }
        else if (pattern[pattern.Length - 1] == '?')
        {
            return MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input.Substring(0, input.Length - 1));
        }
        else if (pattern[0] == '*')
        {
            if (MatchWildcardString(pattern.Substring(1), input))
            {
                return true;
            }
            else
            {
                return MatchWildcardString(pattern, input.Substring(1));
            }
        }
        else if (pattern[pattern.Length - 1] == '*')
        {
            if (MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input))
            {
                return true;
            }
            else
            {
                return MatchWildcardString(pattern, input.Substring(0, input.Length - 1));
            }
        }
        else if (pattern[0] == input[0])
        {
            return MatchWildcardString(pattern.Substring(1), input.Substring(1));
        }
        return false;
    }

【问题讨论】:

  • 你怎么称呼这个Search方法?你是在TextBoxTextChanged 事件处理程序中调用它吗?
  • /// /// 获取/设置当前搜索字符串 /// public string SearchString { get { return this._searchString; } 设置 { if (this._searchString != value) { this._searchString = value; RaisePropertyChanged("SearchString");搜索(搜索字符串); } } }
  • 所以每次击键都会调用Search 方法?你能确认一下吗?
  • 是的,每次击键都会调用它。例如,如果我输入 T 它会调用它然后我输入 * 它会再次调用它。
  • 如果您的搜索在每次击键时触发,那似乎是什么问题?我假设您的 DataGrid 绑定到您可以访问的某个基础集合。而且您可以在 Search 方法本身中将星号附加到您的 inputSearchString 上,不是吗?

标签: c# wpf wpfdatagrid


【解决方案1】:

我把你正在做的事情的一个简单的形式放在一起。我将 XAML 和 ViewModel 放在这里。我基本上创建了一个客户列表,然后将该列表复制到过滤列表中。然后我将我的 DataGrid 绑定到过滤列表。

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>

    <TextBox x:Name="txtSearch" Grid.Row="0" FontSize="14" Margin="10, 0, 10, 0" Height="28" Width="74"
             Text="{Binding Path=SearchString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

    <DataGrid Grid.Row="1" AutoGenerateColumns="True" ItemsSource="{Binding FilteredClients, Mode=TwoWay}" SelectedItem="{Binding SelectedClient}" />
</Grid>

    public class MainViewModel : ViewModelBase
{
    public MainViewModel(IDataService dataService)
    {
        allClients = new List<Client>();
        filteredClients = new List<Client>();
        GetAll();
    }


    public const string SearchStringPropertyName = "SearchString";
    private string searchString = string.Empty;
    public string SearchString
    {
        get
        {
            return searchString;
        }

        set
        {
            if (searchString == value)
            {
                return;
            }

            searchString = value;
            if (searchString == string.Empty)
            {
                filteredClients = allClients;
            }
            else
            {
                FilterAll();
            }
            RaisePropertyChanged(FilteredClientsPropertyName);
            RaisePropertyChanged(SearchStringPropertyName);
        }
    }

    public const string AllClientsPropertyName = "AllClients";
    private List<Client> allClients;
    public List<Client> AllClients
    {
        get
        {
            return allClients;
        }

        set
        {
            if (allClients == value)
            {
                return;
            }

            RaisePropertyChanging(AllClientsPropertyName);
            allClients = value;
            RaisePropertyChanged(AllClientsPropertyName);
        }
    }

    public const string FilteredClientsPropertyName = "FilteredClients";
    private List<Client> filteredClients;
    public List<Client> FilteredClients
    {
        get
        {
            return filteredClients;
        }

        set
        {
            if (filteredClients == value)
            {
                return;
            }

            RaisePropertyChanging(FilteredClientsPropertyName);
            filteredClients = value;
            RaisePropertyChanged(FilteredClientsPropertyName);
        }
    }

    public const string SelectedClientPropertyName = "SelectedClient";
    private Client selectedClient;
    public Client SelectedClient
    {
        get
        {
            return selectedClient;
        }

        set
        {
            if (selectedClient == value)
            {
                return;
            }

            RaisePropertyChanging(SelectedClientPropertyName);
            selectedClient = value;
            RaisePropertyChanged(SelectedClientPropertyName);
        }
    }

    private void GetAll()
    {
        AddClient("sally", "may", 10000);
        AddClient("bert", "benning", 10000);
        AddClient("ernie", "manning", 10000);
        AddClient("lisa", "ann", 10000);
        AddClient("michael", "douglas", 10000);
        AddClient("charlie", "sheen", 10000);

        filteredClients = allClients;
    }

    private void AddClient(String firstname, String lastname, Double salary)
    {
        Client clientadd = new Client();
        clientadd.FirstName = firstname;
        clientadd.LastName = lastname;
        clientadd.Salary = salary;

        allClients.Add(clientadd);
    }

    private void FilterAll()
    {
        filteredClients = (from c in allClients where c.FullName.Contains(searchString) orderby c.FullName select c).ToList();
    }

}

public class Client
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public Double Salary { get; set; }
    public String FullName { get { return LastName + ", " + FirstName; } }

    public List<Client> Clients { get; set; }

}

我不需要星号,我的搜索非常简单。我希望这会有所帮助。

【讨论】:

  • 这绝对比我的方法更直接。我会试一试。
【解决方案2】:

这需要完全匹配

if (term.ToLower().Equals(value.ToString().ToLower()))

将其更改为“StartsWidth”即可。

if (value.ToString().ToLower().StartsWith(term.ToLower())

如果您键入“t”,则值为“thursday”的属性将匹配。

或者试试Contains是不是你想要的。

if (value.ToString().ToLower().Contains(term.ToLower())

【讨论】:

  • 我将其更改为 StartsWith 但现在它根本不过滤。在调试时,字符串 SearchString 包含 T 的值。我什至将 * 放在 T 之后,它不会只过滤掉那些记录。
  • 我尝试了包含但我编写代码的方式仍然需要我放置 *,而我试图摆脱的是一起使用 *。我确信有一种更简单的方法可以从数据网格中的数据库中搜索记录而不使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 2013-03-08
  • 2014-10-14
  • 1970-01-01
相关资源
最近更新 更多