如何使用XAML中的TextBox和ViewModel,MVVM Light中的代码过滤ListBox

时间:2015-07-24 09:07:05

标签: c# xaml mvvm listbox

当用户在ListBox中输入时,有哪些代码可用于过滤我的TextBox

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

1 个答案:

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