티스토리 뷰

C#/WPF

WPF tutorial - The ComboBox control

광그로 2017. 7. 3. 22:01

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(nullnull);
            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



공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
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
글 보관함