1. ObservableCollecton 에서 View 생성
2. 생성된 view에 filter 적용
3. 조건 변경시 View refresh
완전히 일치하는 데이터만 보이게 필터링함 , 띄어쓰기 기준으로 다른 여러 데이터 필터링
xaml
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="3*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Name :" FontSize="15" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBox Grid.Row="0" Grid.Column="1" Margin="5" Text="{Binding Filter , UpdateSourceTrigger=PropertyChanged}"/>
<ListView Grid.Row="1" Grid.ColumnSpan="3" Grid.RowSpan="2" Margin="5" Name="sListView" ItemsSource="{Binding Collection}" >
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="{Binding name}" Header="name" Width="50"/>
<GridViewColumn DisplayMemberBinding="{Binding age}" Header="age" Width="50"/>
<GridViewColumn DisplayMemberBinding="{Binding java}" Header="java" Width="50"/>
<GridViewColumn DisplayMemberBinding="{Binding csharp}" Header="csharp" Width="50"/>
<GridViewColumn DisplayMemberBinding="{Binding python}" Header="python" Width="50"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
c#
public class MainViewModel
{
List<Student> students = null;
public MainViewModel()
{
student = new ObservableCollection<Student>();
ShowListView();
CollectionViewSource = new CollectionViewSource();
CollectionViewSource.Source = this.student;
CollectionViewSource.Filter += TextFilter;
}
private string _filter = string.Empty;
public string Filter
{
get
{
return _filter;
}
set
{
_filter = value;
OnFilterChanged();
}
}
public void TextFilter(object sender, FilterEventArgs e)
{
Student students = (Student)e.Item;
if (string.IsNullOrWhiteSpace(this.Filter) || this.Filter.Length == 0)
e.Accepted = true;
else
{
if (Filter.Contains(' '))
{
string[] InputIPs = Filter.Split(' ');
e.Accepted = Array.IndexOf(InputIPs, students.name) >= 0;
}
else
{
e.Accepted = students.name.Equals(Filter);
}
}
}
public void ShowListView()
{
student.Add(new Student() { name = "jin", csharp = 100, java = 60, python = 70, age = 20 });
student.Add(new Student() { name = "sin", csharp = 40, java = 30, python = 70, age = 20 });
student.Add(new Student() { name = "young", csharp = 30, java = 80, python = 65, age = 20 });
student.Add(new Student() { name = "kim", csharp = 50, java = 70, python = 70, age = 20 });
student.Add(new Student() { name = "song", csharp = 75, java = 60, python = 100, age = 20 });
}
private ObservableCollection<Student> student { get; set; }
public CollectionViewSource CollectionViewSource { get; set; }
public ICollectionView Collection
{
get
{
return CollectionViewSource.View;
}
}
public void OnFilterChanged()
{
CollectionViewSource.View.Refresh();
}
}
public class Student
{
public string name { get; set; }
public int java { get; set; }
public int csharp { get; set; }
public int python { get; set; }
public int age { get; set; }
}
'WPF' 카테고리의 다른 글
[ wpf ] listview column header split (0) | 2022.12.30 |
---|---|
[ wpf ] wpf Live chart _ Stack chart 1초마다 업데이트 해보기 (1) | 2022.12.26 |
[ wpf ] DataTemplate 속성 이름에 접근하는 방법 / 클릭 된 라디오 버튼 이름 가져오기 (0) | 2022.11.24 |
[ wpf ] Property Grid 만드는 법 (0) | 2022.11.24 |
[ wpf ] Live Chart _ Doughnut Chart 사용해보기 (0) | 2021.11.26 |