티스토리 뷰
http://www.wpf-tutorial.com/list-controls/combobox-control/
1 2 3 4 5 6 7 8 9 | <StackPanel Margin="10"> <ComboBox> <ComboBoxItem> <TextBlock FontWeight="Bold" Foreground="Red">itme 1</TextBlock> </ComboBoxItem> <ComboBoxItem IsSelected="True">item 2</ComboBoxItem> <ComboBoxItem>item 3</ComboBoxItem> </ComboBox> </StackPanel> | cs |
ComboBoxItem은 StackPanel 등을 사용하여 image나 Text를 넣을 수 있습니다.
Data Binding the ComboBox
1 2 3 4 5 6 7 8 9 10 11 12 | <StackPanel Margin="10"> <ComboBox Name="cmbColors"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Rectangle Fill="{Binding Name}" Width="16" Height="16" Margin="0,2,5,2"/> <TextBlock Text="{Binding Name}"/> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> </StackPanel> | cs |
1 2 3 4 5 | public MainWindow() { InitializeComponent(); cmbColors.ItemsSource = typeof(Colors).GetProperties(); } | cs |
IsEditable
1 | <ComboBox Name="cmbColors" IsEditable ="True"> | cs |
IsEditable="True" 속성을 사용하면, ComboBox에 원하는 값을 입력 시, 해당 문자열과 일치하는 Item이 선택됩니다
대소문자의 구분을 원한다면, IsTextSearchCaseSensitive 에 true 값을 넣어줍니다.
Working with ComboBox selection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <StackPanel Margin="10"> <ComboBox Name="cbColors" SelectionChanged="cbColors_SelectionChanged"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Rectangle Fill="{Binding Name}" Width="16" Height="16" Margin="0,2,5,2" /> <TextBlock Text="{Binding Name}" /> </StackPanel> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox> <WrapPanel Margin="15" HorizontalAlignment="Center"> <Button Name="btnPrevious" Click="btnPrevious_Click" Width="55">Previous</Button> <Button Name="btnNext" Click="btnNext_Click" Margin="5,0" Width="55">Next</Button> <Button Name="btnBlue" Click="btnBlue_Click" Width="55">Blue</Button> </WrapPanel> </StackPanel> | 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 | using System.Reflection; public MainWindow() { InitializeComponent(); cbColors.ItemsSource = typeof(Colors).GetProperties(); } private void cbColors_SelectionChanged(object sender, SelectionChangedEventArgs e) { Color seletectColor = (Color)(cbColors.SelectedItem as PropertyInfo).GetValue(null, null); this.Background = new SolidColorBrush(seletectColor); } private void btnPrevious_Click(object sender, RoutedEventArgs e) { if (cbColors.SelectedIndex > 0) cbColors.SelectedIndex = cbColors.SelectedIndex - 1; } private void btnNext_Click(object sender, RoutedEventArgs e) { if (cbColors.SelectedIndex < cbColors.Items.Count - 1) cbColors.SelectedIndex = cbColors.SelectedIndex + 1; } private void btnBlue_Click(object sender, RoutedEventArgs e) { cbColors.SelectedItem = typeof(Colors).GetProperty("Blue"); } | cs |
'C# > WPF' 카테고리의 다른 글
WPF tutorial - ListView, data binding and ItemTemplate (0) | 2017.07.03 |
---|---|
WPF tutorial - A simple ListView example (0) | 2017.07.03 |
WPF tutorial - The ListBox control (0) | 2017.07.03 |
WPF tutorial - The ItemsControl (1) | 2017.07.02 |
WPF tutorial - Using the WPF TabControl (0) | 2017.07.02 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Grid
- WPF
- 그래프
- CustomCollectionViewCell
- Cell Animation
- Add TapGesture
- Fakebook
- BFS
- 백준
- MVVM
- 코딩야학
- dfs
- command
- BOJ
- 생활코딩
- Custom Cell
- C++
- DP
- 타일링
- UIView Animation
- 스택
- 백준온라인
- FEED
- 객체
- 데이터 바인딩
- CollectionView
- XAML
- listview
- 문자열
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함