티스토리 뷰
http://www.wpf-tutorial.com/list-controls/listbox-control/
초기에 써놓은 게 날아갔습니다.. 다시 작성하기 귀차나아 ~.~
중요 요점만 적으면 ListBox는 ItemsContorl과 다르게 선택항목을 다룬다!
Data binding the ListBox
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <Grid Margin="10"> <ListBox Name="lbTodoList" HorizontalContentAlignment="Stretch"> <ListBox.ItemTemplate> <DataTemplate> <Grid Margin="0,2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="100" /> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Title}" /> <ProgressBar Grid.Column="1" Minimum="0" Maximum="100" Value="{Binding Completion}" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </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(); List<TodoItem> items = new List<TodoItem>(); items.Add(new TodoItem() { Title = "Complete this WPF tutorial", Completion = 45 }); items.Add(new TodoItem() { Title = "Learn C#", Completion = 80 }); items.Add(new TodoItem() { Title = "Wash the car", Completion = 0 }); lbTodoList.ItemsSource = items; } } public class TodoItem { public string Title { get; set; } public int Completion { get; set; } } | cs |
Working with ListBox selection
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 | <DockPanel Margin="10"> <StackPanel DockPanel.Dock="Right" Margin="10,0"> <StackPanel.Resources> <Style TargetType="Button"> <Setter Property="Margin" Value="0,0,0,5"/> </Style> </StackPanel.Resources> <TextBlock FontWeight="Bold" Margin="0,0,0,10">ListBox selection</TextBlock> <Button Name="btnShowSelectedItem" Click="btnShowSelectedItem_Click">Show selected</Button> <Button Name="btnSelectLast" Click="btnSelectLast_Click">Select last</Button> <Button Name="btnSelectNext" Click="btnSelectNext_Click">Select next</Button> <Button Name="btnSelectCSharp" Click="btnSelectCSharp_Click">Select C#</Button> <Button Name="btnSelectAll" Click="btnSelectAll_Click">Select all</Button> </StackPanel> <ListBox Name="lbTodoList" HorizontalContentAlignment="Stretch" SelectionMode="Extended" SelectionChanged="lbTodoList_SelectionChanged"> <ListBox.ItemTemplate> <DataTemplate> <Grid Margin="0,2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="100" /> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Title}"/> <ProgressBar Grid.Column="1" Minimum="0" Maximum="100" Value="{Binding Completion}"/> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </DockPanel> | 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 | public MainWindow() { InitializeComponent(); List<TodoItem> items = new List<TodoItem>(); items.Add(new TodoItem() { Title = "Complete this WPF tutorial", Completion = 45 }); items.Add(new TodoItem() { Title = "Learn C#", Completion = 80 }); items.Add(new TodoItem() { Title = "Wash the car", Completion = 0 }); lbTodoList.ItemsSource = items; } private void lbTodoList_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (lbTodoList.SelectedItem != null) this.Title = (lbTodoList.SelectedItem as TodoItem).Title; } private void btnShowSelectedItem_Click(object sender, RoutedEventArgs e) { foreach (object o in lbTodoList.SelectedItems) MessageBox.Show((o as TodoItem).Title); } private void btnSelectLast_Click(object sender, RoutedEventArgs e) { lbTodoList.SelectedIndex = lbTodoList.Items.Count - 1; } private void btnSelectNext_Click(object sender, RoutedEventArgs e) { int nextIndex = 0; if ((lbTodoList.SelectedIndex >= 0) && (lbTodoList.SelectedIndex < (lbTodoList.Items.Count - 1))) nextIndex = lbTodoList.SelectedIndex + 1; lbTodoList.SelectedIndex = nextIndex; } private void btnSelectCSharp_Click(object sender, RoutedEventArgs e) { foreach (object o in lbTodoList.Items) { if ((o is TodoItem) && ((o as TodoItem).Title.Contains("C#"))) { lbTodoList.SelectedItem = o; break; } } } private void btnSelectAll_Click(object sender, RoutedEventArgs e) { foreach (object o in lbTodoList.Items) lbTodoList.SelectedItems.Add(o); } } public class TodoItem { public string Title { get; set; } public int Completion { get; set; } } | cs |
ListBox의 SelectionMode="Extended" 를 수정하여 다중선택이 가능하도록 하였습니다.
[CTRL] 또는 [SHIFT]를 누른 상태로 클릭을 하게 되면 중복선택이 가능합니다.
'C# > WPF' 카테고리의 다른 글
WPF tutorial - A simple ListView example (0) | 2017.07.03 |
---|---|
WPF tutorial - The ComboBox control (0) | 2017.07.03 |
WPF tutorial - The ItemsControl (1) | 2017.07.02 |
WPF tutorial - Using the WPF TabControl (0) | 2017.07.02 |
WPF tutorial - The ProgressBar control (0) | 2017.07.02 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- BFS
- Grid
- 문자열
- DP
- Custom Cell
- 객체
- 스택
- dfs
- CollectionView
- 생활코딩
- UIView Animation
- listview
- C++
- BOJ
- CustomCollectionViewCell
- 백준온라인
- FEED
- 타일링
- 백준
- Cell Animation
- 데이터 바인딩
- 그래프
- WPF
- Fakebook
- MVVM
- XAML
- 코딩야학
- Add TapGesture
- command
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함