当用户在ListBox
中输入时,有哪些代码可用于过滤我的TextBox
?
我ListBox
中的数据来自数据库。我使用RelayCommand
获取Event
的所有详细信息,然后将详细信息放在ObservableCollection
中。然后将我的ListBox
绑定到ObservableCollection
和TextBlock
以显示Event Names
。
XAML代码:
<TextBox x:Name="txtSearch" Text="{Binding HomePage.TxtEntered , Mode=TwoWay}" Background="White" FontSize="30" Height="57" Margin="19,10,19,0" Grid.Row="1" />
<Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,72,19,0">
<ListBox Background="Black" x:Name="listBox" FontSize="26" Margin="0,10,0,0" LayoutUpdated="listbox_layoutUpdate" ItemsSource="{Binding HomePage.SearchEventCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="txtEventName" TextWrapping="Wrap" Text="{Binding EventName , UpdateSourceTrigger=PropertyChanged}" Tapped="txtEventName_Tapped" IsTapEnabled="True" Foreground="White" Width="300" Margin="10,15,0,0" Height="55"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
RelayCommand
中的 ViewModel
:
private RelayCommand _eventSearch;
/// <summary>
/// Gets the EventSearch.
/// </summary>
public RelayCommand EventSearch
{
get
{
return _eventSearch
?? (_eventSearch = new RelayCommand(
async() =>
{
SearchEventCollection.Clear();
var eventList = await App.MobileService.GetTable<Event>().ToListAsync();
foreach (Event ename in eventList)
{
SearchEventCollection.Add(new Event
{
Id = ename.Id,
EventName = ename.EventName,
Date = ename.Date,
Location = ename.Location,
Desc = ename.Desc
});
}
}));
}
}
ObservableCollection
:
private static ObservableCollection<Event> _searchEventCollection = new ObservableCollection<Event>();
public static ObservableCollection<Event> SearchEventCollection
{
get { return _searchEventCollection; }
set { _searchEventCollection = value; }
}
PropertyChange
中的 ViewModel
:
public const string TxtEnteredPropertyName = "TxtEntered";
private string _txtEntered;
/// <summary>
/// Sets and gets the TxtEntered property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string TxtEntered
{
get
{
return _txtEntered;
}
set
{
if (_txtEntered == value)
{
return;
}
_txtEntered = value;
RaisePropertyChanged(TxtEnteredPropertyName);
}
}
答案 0 :(得分:0)
尝试使用外卡搜索:
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(EventName);
var display = SearchEventCollection
.Where<string>(item => regEx.IsMatch(item.EventName))
.ToList<string>();
或
var display = SearchEventCollection.Where(q=>q.EventName.ToLower().Contains(EventName));