티스토리 뷰

http://www.wpf-tutorial.com/listview-control/listview-data-binding-item-template/


1
2
3
<Grid>
        <ListView Margin="10" Name="lvDataBinding"></ListView>
    </Grid>
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
            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 });
            items.Add(new User() { Name = "Sammy Doe", Age = 13 });
            lvDataBinding.ItemsSource = items;
        }
    }
 
    public class User
    {
        public string Name { get; set; }
 
        public int Age { get; set; }
    }
cs



데이터 표시 방법에 대한 증거가 없으므로, User에서 Tostring() 메서드를 호출하여 정보를 나타내는데 사용합니다.

1
2
3
4
5
6
7
8
9
10
11
 public class User
    {
        public string Name { get; set; }
 
        public int Age { get; set; }
 
        public override string ToString()
        {
            return "이름 : " + this.Name + ", " + this.Age + "살";
        }
    }
cs




하지만, 위의 경우 오로지 string에만 의존하게 되여 유연하지않습니다. 


ListView with an ItemTemplate


DataTemplate을 사용하여 binding한 예제입니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 <Grid>
        <ListView Margin="10" Name="lvDataBinding">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="Name: " />
                        <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                        <TextBlock Text=", " />
                        <TextBlock Text="Age: " />
                        <TextBlock Text="{Binding Age}" FontWeight="Bold" />
                        <TextBlock Text=" (" />
                        <TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                        <TextBlock Text=")" />
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
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
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
            InitializeComponent();
            List<User> items = new List<User>();
            items.Add(new User() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com" });
            items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
            items.Add(new User() { Name = "Sammy Doe", Age = 13, Mail = "sammy.doe@gmail.com" });
            lvDataBinding.ItemsSource = items;
        }
    }
 
    public class User
    {
        public string Name { get; set; }
 
        public int Age { get; set; }
 
        public string Mail { get; set; }
 
    }
cs





공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
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
글 보관함