본문 바로가기

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

WPF & C# - Listbox, List<string> ( 리스트박스 / 추가 / 제거 / binding / 바인딩)

728x90
반응형

 

 

 

 WPF & C# - Listbox, List<string> ( 리스트박스 / 추가 / 제거 / binding / 바인딩)

 

 

Listbox.zip
다운로드

 

 

 

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
<Window x:Class="Array.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:Array"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="519.5">
    <Grid>
        <CheckBox x:Name="ListItem01" Content="ListItem01" HorizontalAlignment="Left" Margin="180,10,0,0" VerticalAlignment="Top" Click="chk_Click"/>
        <CheckBox x:Name="ListItem02" Content="ListItem02" HorizontalAlignment="Left" Margin="180,30,0,0" VerticalAlignment="Top" Click="chk_Click"/>
        <CheckBox x:Name="ListItem03" Content="ListItem03" HorizontalAlignment="Left" Margin="180,50,0,0" VerticalAlignment="Top" Click="chk_Click"/>
        <CheckBox x:Name="ListItem04" Content="ListItem04" HorizontalAlignment="Left" Margin="180,70,0,0" VerticalAlignment="Top" Click="chk_Click"/>
        <CheckBox x:Name="ListItem05" Content="ListItem05" HorizontalAlignment="Left" Margin="180,90,0,0" VerticalAlignment="Top" Click="chk_Click"/>
        <Label x:Name="lbl" Content="Label" HorizontalAlignment="Left" Margin="103,4,0,0" VerticalAlignment="Top"/>
        <Button x:Name="btn" Content="source Binding" HorizontalAlignment="Left" Margin="180,360,0,0" VerticalAlignment="Top" Width="132" Click="btn_Click"/>
        <ListBox x:Name="lst" HorizontalAlignment="Left" Height="337" Margin="10,10,0,0" VerticalAlignment="Top" Width="92"/>
        <Button x:Name="btnRemoveListItem" Content="RemoveListItem" HorizontalAlignment="Left" Margin="10,360,0,0" VerticalAlignment="Top" Width="92" Click="btn2_Click"/>
        <Button x:Name="btnRemoveListBoxItem" Content="Remove Item" HorizontalAlignment="Left" Margin="339,385,0,0" VerticalAlignment="Top" Width="92" Click="btnRemoveListBoxItem_Click"/>
        <Button x:Name="btnAddItem" Content="add Item" HorizontalAlignment="Left" Margin="339,360,0,0" VerticalAlignment="Top" Width="92" Click="btnAddItem_Click"/>
        <ListBox x:Name="lst2" HorizontalAlignment="Left" Height="337" Margin="339,10,0,0" VerticalAlignment="Top" Width="92"/>
 
    </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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace Array
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        // List 생성
        List<string> strList = new List<string>();
 
        // item 추가
        private void chk_Click(object sender, RoutedEventArgs e)
        {
            lbl.Content = "";
            CheckBox chk = sender as CheckBox;
 
            strList.Add(chk.Content.ToString());
            foreach (string item in strList) lbl.Content += item + "\n";
            lst.Items.Refresh();   // 새로고침
        }
 
        // source 바인딩
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            lst.ItemsSource = strList;
            lst.SelectionMode = SelectionMode.Single;
        }
 
 
        // List<string> - Select Item Romove
        private void btn2_Click(object sender, RoutedEventArgs e)
        {
            if(lst.SelectedItem !=null)
            {
                strList.RemoveAt(lst.SelectedIndex);
                lst.Items.Refresh();
            }
        }
 
        // Listbox - Add item
        private void btnAddItem_Click(object sender, RoutedEventArgs e)
        {
            lst2.Items.Add("a");
            lst2.Items.Add("b");
            lst2.Items.Add("c");
            lst2.Items.Add("d");
            lst2.Items.Add("e");
            lst2.Items.Add("f");
            lst2.Items.Add("g");
            lst2.SelectionMode = SelectionMode.Multiple;
        }
 
        // Listbox - Select Item Romove
        private void btnRemoveListBoxItem_Click(object sender, RoutedEventArgs e)
        {
            for (int i = lst2.SelectedItems.Count; i > 0; i--)
            {
                int idx = lst2.SelectedIndex;
                lst2.Items.RemoveAt(idx);
            }
        }
    }
}
 
cs
 

 

참고

insurang.tistory.com/551

 

WPF & C# - List 동적 배열 ( 리스트 / array / dictionary )

 WPF & C# - List 동적 배열 추가, 삭제 ( 리스트 / array / dictionary / AddRange / RemoveAll ) 파일읽고 라인별로 배열에 넣기 List list = new List (); string saveFile = @"data.txt"; using (StreamReade..

insurang.tistory.com

 

728x90
반응형