带有DataGrid的WPF应用程序中的C#搜索框
本文关键字:搜索 应用程序 DataGrid WPF 带有 | 更新日期: 2023-09-27 18:27:40
嗨,我在网站上搜索了一下,没有找到我的问题的答案。我读到了这个:http://davidowens.wordpress.com/2009/02/18/wpf-search-text-box/这是从另一个关于wpf搜索框的问题中提出的。我有一个C#WPF应用程序,我正在使用Access DB。在我的数据输入屏幕上,我有一个搜索框和一个显示数据库中所有记录的数据网格。我目前的搜索是有效的,但不是我想象的那样。我希望用户能够开始在搜索框中键入,数据网格中显示的记录列表将开始根据他们键入的内容进行筛选。我写的代码本以为会这样做,但为了让他们进行搜索,他们必须键入:people*,然后点击回车键,它就会显示结果。我想知道有没有一种方法可以修改我的代码,使其不需要星号,并在键入时进行筛选,或者应该以不同的方式编写?我的数据输入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;
}
我把你正在做的事情整理成一个简单的形式。我将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; }
}
我不需要星号,我的搜索非常简单。我希望这能有所帮助。
这需要一个完全匹配的
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())