使用文本框搜索在WPF中创建Listview过滤器

时间:2015-02-24 15:06:21

标签: c# wpf listview filter

我正在使用名为star.xml的XML文件创建一个应用程序,以将我的数据存储在列表视图中。我是c#和编程的新手,需要任何帮助 基本上,我希望能够输入我的搜索文本框(称为“搜索”)和我的列表视图(lstStar)以仅显示匹配的记录。即输入'Audi'只会返回这些项目。 任何帮助都感激不尽 仁

namespace StarinCar
{
    public partial class MainWindow : Window
    {
        int hot = -2;
        int Mildly_Moist = -2;
        int Wet = -4;
        int Very_Wet = -6;
        private ObservableCollection<star> starData;

        public MainWindow()
        {
            InitializeComponent();
            starData = new ObservableCollection<star>();
            lstStar.ItemsSource = starData;

            try
            {
                XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<star>));
                using (StreamReader rd = new StreamReader("star.xml"))
                {
                    starData = xs.Deserialize(rd) as ObservableCollection<star>;
                }
            }
            catch
            {

            }

            lstStar.ItemsSource = starData;

            lblAverage.Content = starData.Average(i => i.time).ToString();
            lblFastest.Content = starData.Min(i => i.time).ToString();
            lblSlowest.Content = starData.Max(i => i.time).ToString();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            star newStar = new star();

            newStar.firstName = txtName.Text;
            newStar.time = int.Parse(txtTime.Text);  
            newStar.car = txtCar.Text;
            newStar.track = txtTrack.Text;

            starData.Add(newStar);

            if (txtTrack.Text.Contains("Hot") || (txtTrack.Text.Contains("hot") == true))
            {
                newStar.time = int.Parse(txtTime.Text) + hot;
            }

            if (txtTrack.Text.Contains("Mildly Moist") || (txtTrack.Text.Contains("mildly moist")) == true)
            {
                newStar.time = int.Parse(txtTime.Text) + Mildly_Moist;
            }

            if (txtTrack.Text.Contains("Wet") || (txtTrack.Text.Contains("wet") == true))
            {
                newStar.time = int.Parse(txtTime.Text) + Wet;
            }

            if (txtTrack.Text.Contains("Very Wet") || (txtTrack.Text.Contains("very wet")) == true)
            {
                newStar.time = int.Parse(txtTime.Text) + Very_Wet;
            }
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<star>));
            using (StreamWriter wr = new StreamWriter("star.xml"))
            {
                xs.Serialize(wr, starData);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用ICollectionView。所以你会得到你的“整体”  明星集合'starData'。但是你的listbox itemssource将绑定到这样的东西:

public ICollectionView FilteredStars
    {
        get
        {
            ICollectionView source = CollectionViewSource.GetDefaultView(starData);
            source.Filter = new Predicate<object>(FilterStars);

            return source;
        }
    }

这里进行过滤的逻辑:

private bool FilterStars(object item)
    {
        bool b = false;
        star a = item as star;

        if (a != null)
        {
            if (a.Name.Contains(searchBoxText))  //your filter logic here
            {
                   b = true;
            }
            else if String.IsNullOrWhiteSpace(searchBoxText)
            {
                   b = true;
            }
        }

        return b;
    }

基本上,你有你的主集合,然后是一些过滤你的主集合到过滤集合的逻辑,这就是你应该将列表框的itemssource设置为。到目前为止,这是假设您要在搜索文本框中添加某种属性更改,然后单击按钮,如“搜索”,然后告诉列表检查并重新填充以匹配搜索项。

答案 1 :(得分:-1)

这是我在类似应用程序中过滤的方式

    public IEnumerable<string> PastEntries1
    {
        get
        {
            if(string.IsNullOrEmpty(textValue))
            {
                return FieldDefString.PastEntries;
            }
            else 
            {
                return FieldDefString.PastEntries.Where(x => x.StartsWith(textValue, StringComparison.OrdinalIgnoreCase));
            }
        }
    }