您好我在网站上搜索过,并没有找到我的问题的答案。我读过这篇文章:http://davidowens.wordpress.com/2009/02/18/wpf-search-text-box/是根据某人对wpf搜索框提出的另一个问题提出的。 我有一个C#WPF应用程序,我正在使用Access DB。在我的数据输入屏幕上,我有一个搜索框和一个显示数据库中所有记录的数据网格。我目前的搜索工作但不是我认为的方式。我希望用户能够在搜索框中开始输入,并且数据网格中显示的记录列表将根据他们键入的内容开始过滤。我写的代码我认为会这样做,但为了让他们进行搜索,他们必须例如键入: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;
}
答案 0 :(得分:0)
这需要完全匹配
if (term.ToLower().Equals(value.ToString().ToLower()))
将其更改为&#34; StartsWidth&#34;可以做到这一点。
if (value.ToString().ToLower().StartsWith(term.ToLower())
如果您键入&#34; t&#34;,那么值为&#34;星期四&#34;会匹配。
或尝试Contains
是您想要的。
if (value.ToString().ToLower().Contains(term.ToLower())
答案 1 :(得分:0)
我把你正在做的一个简单形式放在一起。我在这里放置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; }
}
我不需要星号,我的搜索非常简单。我希望这会有所帮助。