[英]C# Searchbox in a WPF Application with a DataGrid
嗨,我在该网站上进行搜索,但没有找到关于我的问题的答案。 我读了这篇文章: http : //davidowens.wordpress.com/2009/02/18/wpf-search-text-box/ ,这是有人对wpf搜索框有另一个疑问的建议。 我有一个使用Access DB的C#WPF应用程序。 在我的数据输入屏幕上,我有一个搜索框和一个数据网格,用于显示数据库中的所有记录。 我目前进行的搜索有效,但不符合我的预期。 我希望用户能够开始在搜索框中键入内容,并且数据网格中显示的记录列表将开始根据其键入内容进行过滤。 我想写的代码可以做到这一点,但是为了使他们能够进行搜索,他们必须例如输入: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;
}
这需要完全匹配
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())
我整理了一下您正在做的事情的简单形式。 我在这里放置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; }
}
我不需要星号,搜索非常简单。 我希望这有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.