ABOUT ME

Today
Yesterday
Total
  • WPF ComboBox control
    IT Story/C# & WPF 2019. 12. 25. 16:35
    반응형

    The ComboBox control is in many ways like the ListBox control, but takes up a lot less space, because the list of items is hidden when not needed. The ComboBox control is used many places in Windows, but to make sure that everyone knows how it looks and works, we'll jump straight into a simple example:

     

    <Window x:Class="WpfTutorialSamples.ComboBox_control.ComboBoxSample"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="ComboBoxSample" Height="150" Width="200">
        <StackPanel Margin="10">
            <ComboBox>
                <ComboBoxItem>ComboBox Item #1</ComboBoxItem>
                <ComboBoxItem IsSelected="True">ComboBox Item #2</ComboBoxItem>
                <ComboBoxItem>ComboBox Item #3</ComboBoxItem>
            </ComboBox>
        </StackPanel>
    </Window>

    In the screenshot, I have activated the control by clicking it, causing the list of items to be displayed. As you can see from the code, the ComboBox, in its simple form, is very easy to use. All I've done here is manually add some items, making one of them the default selected item by setting the IsSelected property on it.

    Custom content

    In the first example we only showed text in the items, which is pretty common for the ComboBox control, but since the ComboBoxItem is a ContentControl, we can actually use pretty much anything as content. Let's try making a slightly more sophisticated list of items:

     

    <Window x:Class="WpfTutorialSamples.ComboBox_control.ComboBoxCustomContentSample"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="ComboBoxCustomContentSample" Height="150" Width="200">
        <StackPanel Margin="10">
            <ComboBox>
                <ComboBoxItem>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/WpfTutorialSamples;component/Images/bullet_red.png" />
                        <TextBlock Foreground="Red">Red</TextBlock>
                    </StackPanel>
                </ComboBoxItem>
                <ComboBoxItem>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/WpfTutorialSamples;component/Images/bullet_green.png" />
                        <TextBlock Foreground="Green">Green</TextBlock>
                    </StackPanel>
                </ComboBoxItem>
                <ComboBoxItem>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/WpfTutorialSamples;component/Images/bullet_blue.png" />
                        <TextBlock Foreground="Blue">Blue</TextBlock>
                    </StackPanel>
                </ComboBoxItem>
            </ComboBox>
        </StackPanel>
    </Window>

    For each of the ComboBoxItem's we now add a StackPanel, in which we add an Image and a TextBlock. This gives us full control of the content as well as the text rendering, as you can see from the screenshot, where both text color and image indicates a color value.

    Data binding the ComboBox

    As you can see from the first examples, manually defining the items of a ComboBox control is easy using XAML, but you will likely soon run into a situation where you need the items to come from some kind of data source, like a database or just an in-memory list. Using WPF data binding and a custom template, we can easily render a list of colors, including a preview of the color:

     

    It's actually quite simple: In the Code-behind, I obtain a list of all the colors using a Reflection based approach with the Colors class. I assign it to the ItemsSource property of the ComboBox, which then renders each color using the template I have defined in the XAML part.

    Each item, as defined by the ItemTemplate, consists of a StackPanel with a Rectangle and a TextBlock, each bound to the color value. This gives us a complete list of colors, with minimal effort - and it looks pretty good too, right?

    IsEditable

    In the first examples, the user was only able to select from our list of items, but one of the cool things about the ComboBox is that it supports the possibility of letting the user both select from a list of items or enter their own value. This is extremely useful in situations where you want to help the user by giving them a pre-defined set of options, while still giving them the option to manually enter the desired value. This is all controlled by the IsEditable property, which changes the behavior and look of the ComboBox quite a bit:

    반응형

    'IT Story > C# & WPF' 카테고리의 다른 글

    댓글

Designed by Tistory.