티스토리 뷰

C#/WPF

WPF tutorial - ListView with a GridView

광그로 2017. 7. 3. 23:36

http://www.wpf-tutorial.com/listview-control/listview-with-gridview/


ListBox와 ListView의 가장 큰 차이점은 GridView의 사용입니다.

1
2
3
4
5
6
7
8
9
10
11
<Grid>
        <ListView Margin="10" Name="lvUsers">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}"></GridViewColumn>
                    <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}"></GridViewColumn>
                    <GridViewColumn Header="Mail" Width="200" DisplayMemberBinding="{Binding Mail}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            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 = 7, Mail = "sammy.doe@gmail.com" });
            lvUsers.ItemsSource = items;
        }
    }
    public class User
    {
        public string Name { get; set; }
 
        public int Age { get; set; }
 
        public string Mail { get; set; }
    }
cs



Header: 열의 기준, DB로 따지면 attribute값이 됩니다.

DisplayMemberBinding: 바인딩되어 표시될 값을 지정합니다.


Templated cell content


CellTemplate를 이용하여 DisplayMemberBindig의 제한적인 출력을 유연하게할 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<Grid>
        <ListView Margin="10" Name="lvUsers">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                    <GridViewColumn Header="Mail" Width="150">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
cs

1
2
3
 <GridViewClumn>
 <GridViewColumn.CellTemplate>
<DataTemplate>
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
글 보관함