[C# WPF] WPF ListView 컨트롤 Data Binding(데이터바인딩) 하기

안녕하세요.

 

오늘은 C# WPF에서 ListView 컨트롤 사용 방법에 대해서 알아 보려고 합니다.

그 중에서 ListView 컨트롤을 Data Binding 하여 사용하는 방법에 대해서 알려 드리려고 합니다.

 

예제 코드를 통해서 한번씩 따라해 보시면서 익히시는걸 추천 드리겠습니다!

 

 

그럼 바로 예제를 통해서 ListView 컨트롤에 Data Binding 하는 방법을 보여 드리도록 하겠습니다.

 

MainWindows.xaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<Window x:Class="ListViewDataBinding.MainWindow"
        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"
        xmlns:local="clr-namespace:ListViewDataBinding"
        xmlns:ViewModels="clr-namespace:ListViewDataBinding.ViewModels"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="500">
    
    <Window.DataContext>
        <ViewModels:StudentListViewModel/>
    </Window.DataContext>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
 
        <TextBlock  HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="ListView DataBinding Example" Padding="10" />
        <ListView ItemsSource="{Binding Items}" Name="uiLv_Main" Grid.Row="1" Grid.Column="0" Margin="10" Background="AliceBlue">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Name}"/>
                    <GridViewColumn Header="Age" Width="100" DisplayMemberBinding="{Binding Age}"/>
                    <GridViewColumn Header="PhoneNumber" Width="270" DisplayMemberBinding="{Binding PhoneNumber}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>
 
cs

 

위와 같이 Xaml 코드를 작성하였다면 이제 ListView 컨트롤에 Data Binding 할 데이터를 선언 및 생성할 Model, ViewModel 클래스들을 각각 만들어 보도록 하겠습니다.

 

Student.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
 
namespace ListViewDataBinding
{
    public class StudentList : ObservableCollection<Student>
    {
        /// <summary>
        /// 생성자
        /// </summary>
        public StudentList()
        {
            Add(new Student() { Name = "범범조조", Age = 28, PhoneNumber = "010-2345-2222" });
            Add(new Student() { Name = "안정환", Age = 20, PhoneNumber = "010-4345-2222" });
            Add(new Student() { Name = "아이유", Age = 38, PhoneNumber = "010-5345-2672" });
            Add(new Student() { Name = "정형돈", Age = 21, PhoneNumber = "010-3345-2752" });
            Add(new Student() { Name = "유재석", Age = 74, PhoneNumber = "010-4345-2752" });
            Add(new Student() { Name = "박명수", Age = 54, PhoneNumber = "010-5345-2752" });
            Add(new Student() { Name = "하하", Age = 47, PhoneNumber = "010-8345-2752" });
            Add(new Student() { Name = "광희", Age = 21, PhoneNumber = "010-6745-2752" });
            Add(new Student() { Name = "조세호", Age = 31, PhoneNumber = "010-6345-2752" });
        }
    }
 
    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string PhoneNumber { get; set; }
    }
}
 
cs

 

 

StudentListViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.Text;
 
namespace ListViewDataBinding.ViewModels
{
    public class StudentListViewModel
    {
        private readonly StudentList items;
 
        public StudentListViewModel()
        {
            this.items = new StudentList();
        }
 
        public StudentList Items
        {
            get { return this.items; }
        }
    }
}
 
cs

 

실행 결과

위와 같이 Student 객체 List 정보들이 ListView 컨트롤에 제대로 DataBinding 된 것을 확인하실 수 있습니다.

 

이로써, 오늘은 간단히 WPF ListView 컨트롤 Data Binding 하는 방법에 대해서 알아 보았습니다.

 

감사합니다.^^

728x90

이 글을 공유하기

댓글

Designed by JB FACTORY