如何使用XAML中的TextBox和ViewModel、MVVM Light中的代码筛选ListBox

本文关键字:Light 代码 ListBox 筛选 MVVM XAML 何使用 中的 TextBox ViewModel | 更新日期: 2023-09-27 18:04:21

当用户输入TextBox时,要使用哪些代码来筛选出我的ListBox

我的ListBox中的数据来自数据库。我使用了一个RelayCommand来获取Event的所有细节,然后将这些细节放在ObservableCollection中。然后将我的ListBox绑定到ObservableCollectionTextBlock以显示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>

ViewModel:中的RelayCommand

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

ViewModel:中的PropertyChange

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

如何使用XAML中的TextBox和ViewModel、MVVM Light中的代码筛选ListBox

尝试通配符搜索:

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