728x90
반응형
WPF & C# - Listbox, List<string> ( 리스트박스 / 추가 / 제거 / binding / 바인딩) |
MainWindow.xaml.cs
12345678910111213141516171819202122232425 <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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 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 Romoveprivate void btn2_Click(object sender, RoutedEventArgs e){if(lst.SelectedItem !=null){strList.RemoveAt(lst.SelectedIndex);lst.Items.Refresh();}}// Listbox - Add itemprivate 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 Romoveprivate 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
참고
728x90
반응형
'Programing (프로그래밍) > WPF & C# (C Sharp)' 카테고리의 다른 글
WPF & C# - DataGrid 에 mdb 연결 ( db / connection / connect ) (0) | 2018.05.29 |
---|---|
WPF & C# - 템플릿 내 오브젝트 선택하기 ( Template / object ) (0) | 2018.05.29 |
WPF & C# - foreach vs Join ( 속도 테스트 ) (0) | 2018.05.27 |
WPF & C# - 토글버튼 / 리핏버튼 ( ToggleButton / RepeatButton ) (0) | 2018.05.27 |
WPF & C# - List<string> 동적 배열 ( 리스트 / array / dictionary ) (0) | 2018.05.24 |
WPF & C# - 색상변환 / 브러쉬, 컬러 ( Brush <-> Color / SolidColorBrush / 색상 형변환) (0) | 2018.05.24 |
[자작] WPF & C# - TouchTotalCount ( 터치 수 확인 프로그램 ) (0) | 2018.05.23 |
WPF & C# - 이벤트 라우팅 버블링/터널링/직접전달 테스트 ( Bubbling / Tunneling / direct / e.Handled = true ) (0) | 2018.05.23 |