Using filter method of listview in WPF

Using filter method of listview in WPF

kinakira2013
Advocate Advocate
1,662 Views
4 Replies
Message 1 of 5

Using filter method of listview in WPF

kinakira2013
Advocate
Advocate

Hi,

I want to filter information (Name/Age/..) from textbox and show results in a listview (WPF) as an example from https://wpf-tutorial.com/listview-control/listview-filtering/ . However, There was an error and my program stopped. When I debugged this program, it stopped at the code line:

InitializeComponent();

 Please advise me,

 

listview_filtering_simple.png

 

Thanks & Best Regards,

Kin

0 Likes
Accepted solutions (1)
1,663 Views
4 Replies
Replies (4)
Message 2 of 5

_gile
Consultant
Consultant

Hi,

 

Beyond the fact that your question has nothing to do with AutoCAD programming (which is the subject of this forum), it is impossible to answer your question with so little information.

The code in the link you give works correctly. You certainly copied it wrong.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 5

kinakira2013
Advocate
Advocate

Hi @_gile ,

I mean that I want to create a dialog WPF with a Filter function via AutoCAD command method.

 

       [CommandMethod("SEARCHTEST")]
        public void cmd_SearchTest()
        {            
            var SearchDialog = new Search();
            var result = AcAp.ShowModalWindow(SearchDialog);
        }

 

 

Code-behind:

 

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

namespace Test
{    
    public partial class Search : Window
    {
        public Search()
        {
            InitializeComponent();
            List<User> items = new List<User>();
            items.Add(new User() { Name = "John Doe", Age = 42 });
            items.Add(new User() { Name = "Jane Doe", Age = 39 });
            
            lvSearch.ItemsSource = items;

            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvSearch.ItemsSource);

            view.Filter = User_Filter;            
        }

        private void TxtSearch_TextChanged(object sender,TextChangedEventArgs e)
        {              
            CollectionViewSource.GetDefaultView(lvSearch.ItemsSource).Refresh();            
        }

        private bool User_Filter(object item)
        {
            if (string.IsNullOrEmpty(txtSearch.Text))
            {
                return true;
            }
            else
            {
                return (item as User).Name.IndexOf(txtSearch.Text, StringComparison.OrdinalIgnoreCase)>=0;
            }
        }

        public class User
        {
            public string Name { get; set; }
            public int Age { get; set; }            
        }
   }
}

 

 

When I run this command, an error message appears as bellow photo

Error.png

 

Best Regards,

Kin

0 Likes
Message 4 of 5

_gile
Consultant
Consultant
Accepted solution

It works for me like this:

 

XAML

<Window x:Class="Test.Search"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="FilteringSample" Height="200" Width="300">
    <DockPanel Margin="10">
        <TextBox DockPanel.Dock="Top" Margin="0,0,0,10" Name="txtSearch" TextChanged="TxtSearch_TextChanged" />
        <ListView Name="lvSearch">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                </GridView>
            </ListView.View>
        </ListView>
    </DockPanel>
</Window>

 

Code behind:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Test
{
    public partial class Search : Window
    {
        public Search()
        {
            InitializeComponent();
            List<User> items = new List<User>();
            items.Add(new User() { Name = "John Doe", Age = 42 });
            items.Add(new User() { Name = "Jane Doe", Age = 39 });

            lvSearch.ItemsSource = items;

            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvSearch.ItemsSource);

            view.Filter = User_Filter;
        }

        private void TxtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            CollectionViewSource.GetDefaultView(lvSearch.ItemsSource).Refresh();
        }

        private bool User_Filter(object item)
        {
            if (string.IsNullOrEmpty(txtSearch.Text))
            {
                return true;
            }
            else
            {
                return (item as User).Name.IndexOf(txtSearch.Text, StringComparison.OrdinalIgnoreCase) >= 0;
            }
        }

        public class User
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
}

 

Commands class:

using Autodesk.AutoCAD.Runtime;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace Test
{
    public class Commands
    {
        [CommandMethod("SEARCHTEST")]
        public void cmd_SearchTest()
        {
            var SearchDialog = new Search();
            var result = AcAp.ShowModalWindow(SearchDialog);
        }
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 5

kinakira2013
Advocate
Advocate

Thanks for your test, _gile !

Maybe some causes from my system, I'm trying to find them.

 

Best Regards,

Kin

0 Likes