This gives us aĬomplete list of colors, with minimal effort - and it looks pretty good too, right? IsEditable The ItemsSource property of the ComboBox, which then renders each color using the template I have defined in the XAML part.Įach item, as defined by the ItemTemplate, consists of a StackPanel with a Rectangle and a TextBlock, each bound to the color value. 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. Public partial class ComboBoxDataBindingSample : WindowĬmbColors.ItemsSource = typeof(Colors).GetProperties() Namespace WpfTutorialSamples.ComboBox_control ![]() Using WPF data binding and a custom template, weĬan easily render a list of colors, including a preview of the color: Where you need the items to come from some kind of data source, like a database or just an in-memory list. Data binding the ComboBoxĪs 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 Text rendering, as you can see from the screenshot, where both text color and image indicates a color value. This gives us full control of the content as well as the Let's try making a slightly more sophisticated list of items: įor each of the ComboBoxItem's we now add a StackPanel, in which we add an Image and a TextBlock. 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Ĭan actually use pretty much anything as content. ![]() All I've done here is manually add some items, making one of them the default selected item by setting the IsSelected ![]() As you can see from the code, the ComboBox, in In the screenshot, I have activated the control by clicking it, causing the list of items to be displayed. TheĬomboBox 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: 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |