gpt4 book ai didi

带有 DataGrid 的 WPF 应用程序中的 C# 搜索框

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:29 31 4
gpt4 key购买 nike

您好,我在网站上进行了搜索,但没有找到我的问题的答案。我读了这个: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;
}

最佳答案

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

<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; }

}

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

关于带有 DataGrid 的 WPF 应用程序中的 C# 搜索框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26551175/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com