【问题标题】:How to filter ListBox using TextBox from XAML and code in ViewModel, MVVM Light如何使用 XAML 中的 TextBox 和 ViewModel、MVVM Light 中的代码过滤 ListBox
【发布时间】:2015-07-24 09:07:04
【问题描述】:

当用户输入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>

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

【问题讨论】:

  • 您希望 LINQ 查询使用文本框中的字符过滤列表框。?
  • ok 可以试试
  • 确定要过滤什么 ID?
  • 我要过滤事件名称

标签: c# xaml mvvm listbox


【解决方案1】:

用通配符搜索试试这个:

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

【讨论】:

  • 谢谢,但我不知道在哪里添加此代码。你能帮我通过mvvm 进行过滤吗?我还添加了一个绑定到文本框的propertychange。现在我想过滤列表框
  • Text="{Binding EventName, UpdateSourceTrigger=PropertyChanged}" 以便在每次输入文本时更新
  • 并将该代码放入在文本框中输入任何文本时调用的方法中
  • 不适合我过滤列表框的任何其他选项
  • 你能编辑问题并把你过滤掉的地方放在哪里吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-28
  • 2013-11-10
  • 1970-01-01
  • 2012-04-16
  • 1970-01-01
相关资源
最近更新 更多