본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

ListView 추가/삭제 ( add / remove / delete / del )

728x90
반응형


 ListView 추가/삭제 ( add / remove / delete / del )









[MainWindow.xaml]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<Window
        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:WpfApp1"
        xmlns:Properties="clr-namespace:WpfApp1.Properties" x:Class="WpfApp1.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="521.5" Width="572.968">
    <Grid Margin="10">
        <ListView x:Name="ListView01" HorizontalAlignment="Left" Width="448"  Height="461" VerticalAlignment="Top" Margin="87,0,0,0">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" Width="200" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Age" Width="100" DisplayMemberBinding="{Binding Age}" />
                </GridView>
            </ListView.View>
        </ListView>
        <Button x:Name="btnADD" Content="ADD" HorizontalAlignment="Left" VerticalAlignment="Top" Width="82" Height="41" Click="btnADD_Click"/>
        <Button x:Name="btn_DEL" Content="DEL" HorizontalAlignment="Left" Height="41" Margin="0,46,0,0" VerticalAlignment="Top" Width="82" Click="btn_DEL_Click"/>
    </Grid>
</Window>
 
cs



[MainWindow.xaml.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
35
36
37
38
39
40
41
42
43
using System.Collections.Generic;
using System.Windows;
namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        List<Man> items = new List<Man>();
        public class Man
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
        private void btnADD_Click(object sender, RoutedEventArgs e)
        {
            items.Add(new Man() { Name = "aaa", Age = 555 });
            items.Add(new Man() { Name = "bbb", Age = 444 });
            items.Add(new Man() { Name = "ccc", Age = 333 });
            items.Add(new Man() { Name = "ddd", Age = 222 });
            items.Add(new Man() { Name = "eee", Age = 111 });
            ListView01.ItemsSource = items;
            ListView01.Items.Refresh();
        }
        private void btn_DEL_Click(object sender, RoutedEventArgs e)
        {
            foreach (Man item in ListView01.SelectedItems)
            {
                items.Remove(item);
            }
            ListView01.Items.Refresh();
        }
    }
}
cs



728x90
반응형