|
1 | | -Title: ComboBox |
2 | | -Description: The ComboBox styles |
3 | | ---- |
| 1 | +## Title: ComboBox |
4 | 2 |
|
5 | | -_coming soon_ |
| 3 | +# Default Style |
| 4 | + |
| 5 | +The standard `ComboBox` is automatically styled when you include the MahApps.Metro library. You can enhance it with helper properties from `TextBoxHelper`. |
| 6 | + |
| 7 | +----- |
| 8 | + |
| 9 | +## Watermark |
| 10 | + |
| 11 | +You can add placeholder text, known as a watermark, which appears when the `ComboBox` has no selected item. |
| 12 | + |
| 13 | +Add the following attribute to your `ComboBox`: `mah:TextBoxHelper.Watermark="Your placeholder text..."` |
| 14 | + |
| 15 | +```xml |
| 16 | +<ComboBox mah:TextBoxHelper.Watermark="Please select an item..." |
| 17 | + Width="200"> |
| 18 | + <ComboBoxItem Content="Item 1" /> |
| 19 | + <ComboBoxItem Content="Item 2" /> |
| 20 | +</ComboBox> |
| 21 | +``` |
| 22 | + |
| 23 | +----- |
| 24 | + |
| 25 | +## ClearText Button |
| 26 | + |
| 27 | +A button to clear the selected item can be added to the `ComboBox`. |
| 28 | + |
| 29 | +Set `mah:TextBoxHelper.ClearTextButton="True"` to make the button always visible. |
| 30 | + |
| 31 | +```xml |
| 32 | +<ComboBox mah:TextBoxHelper.ClearTextButton="True" |
| 33 | + mah:TextBoxHelper.Watermark="Please select an item..." |
| 34 | + Width="200"> |
| 35 | + <ComboBoxItem Content="Item 1" /> |
| 36 | + <ComboBoxItem Content="Item 2" /> |
| 37 | +</ComboBox> |
| 38 | +``` |
| 39 | + |
| 40 | +Alternatively, you can bind its visibility to the `SelectedItem` property, so it only appears when an item is selected. |
| 41 | + |
| 42 | +```xml |
| 43 | +<ComboBox mah:TextBoxHelper.ClearTextButton="{Binding Path=SelectedItem, RelativeSource={RelativeSource Mode=Self}, Converter={x:Static mah:IsNotNullConverter.Instance}}" |
| 44 | + mah:TextBoxHelper.Watermark="Please select an item..." |
| 45 | + Width="200"> |
| 46 | + <ComboBoxItem Content="Item 1" /> |
| 47 | + <ComboBoxItem Content="Item 2" /> |
| 48 | +</ComboBox> |
| 49 | +``` |
| 50 | + |
| 51 | +----- |
| 52 | + |
| 53 | +## Floating Watermark |
| 54 | + |
| 55 | +The watermark can be configured to float above the `ComboBox` like a label when an item is selected. This also works for editable `ComboBoxes`. |
| 56 | + |
| 57 | +Add the following attribute to apply this style: `mah:TextBoxHelper.UseFloatingWatermark="True"` |
| 58 | + |
| 59 | +```xml |
| 60 | +<ComboBox mah:TextBoxHelper.UseFloatingWatermark="True" |
| 61 | + mah:TextBoxHelper.Watermark="Please select an item..." |
| 62 | + Width="200"> |
| 63 | + <ComboBoxItem Content="Item 1" /> |
| 64 | + <ComboBoxItem Content="Item 2" /> |
| 65 | +</ComboBox> |
| 66 | +``` |
| 67 | + |
| 68 | +----- |
| 69 | + |
| 70 | +## Custom Button Content |
| 71 | + |
| 72 | +You can replace the default dropdown arrow with custom content, such as an icon. This is useful for creating search or filter-like inputs. |
| 73 | + |
| 74 | +Use the `ButtonCommand`, `ButtonContent`, and `ButtonContentTemplate` properties from `TextBoxHelper` to customize the button. |
| 75 | + |
| 76 | +```xml |
| 77 | +<ComboBox Width="200" |
| 78 | + mah:TextBoxHelper.ButtonCommand="{Binding ControlButtonCommand, Mode=OneWay}" |
| 79 | + mah:TextBoxHelper.ButtonContent="M17.545,15.467l-3.779-3.779..." |
| 80 | + mah:TextBoxHelper.Watermark="Search here..."> |
| 81 | + <mah:TextBoxHelper.ButtonContentTemplate> |
| 82 | + <DataTemplate> |
| 83 | + <mah:PathIcon Width="16" |
| 84 | + Height="16" |
| 85 | + Padding="3" |
| 86 | + Data="{Binding Mode=OneWay}" /> |
| 87 | + </DataTemplate> |
| 88 | + </mah:TextBoxHelper.ButtonContentTemplate> |
| 89 | + <ComboBoxItem Content="Item 1" /> |
| 90 | + <ComboBoxItem Content="Item 2" /> |
| 91 | +</ComboBox> |
| 92 | +``` |
| 93 | + |
| 94 | +----- |
| 95 | + |
| 96 | +## Editable / Auto-Completion |
| 97 | + |
| 98 | +Set `IsEditable="True"` to allow users to type directly into the `ComboBox`. When combined with an `ItemsSource`, this enables auto-completion. |
| 99 | + |
| 100 | +For `ComboBoxes` with a large number of items, use the virtualized style for better performance. |
| 101 | + |
| 102 | +Add the following to a `ComboBox` to apply this style: `Style="{DynamicResource MahApps.Styles.ComboBox.Virtualized}"` |
| 103 | + |
| 104 | +```xaml |
| 105 | +<ComboBox Width="200" |
| 106 | + mah:TextBoxHelper.Watermark="Auto completion" |
| 107 | + DisplayMemberPath="Title" |
| 108 | + IsEditable="True" |
| 109 | + ItemsSource="{Binding Albums}" |
| 110 | + Style="{DynamicResource MahApps.Styles.ComboBox.Virtualized}" /> |
| 111 | +``` |
| 112 | + |
| 113 | +----- |
| 114 | + |
| 115 | +## Grouping |
| 116 | + |
| 117 | +Items within the dropdown can be grouped under custom headers. This is achieved by setting the `<ComboBox.GroupStyle>`. |
| 118 | + |
| 119 | +```xml |
| 120 | +<ComboBox Width="200" |
| 121 | + mah:TextBoxHelper.Watermark="Auto completion with grouping" |
| 122 | + DisplayMemberPath="Title" |
| 123 | + IsEditable="True" |
| 124 | + ItemsSource="{Binding Albums}" |
| 125 | + Style="{DynamicResource MahApps.Styles.ComboBox.Virtualized}"> |
| 126 | + <ComboBox.GroupStyle> |
| 127 | + <GroupStyle> |
| 128 | + <GroupStyle.HeaderTemplate> |
| 129 | + <DataTemplate> |
| 130 | + <TextBlock Text="{Binding Path=Name.Name}" /> |
| 131 | + </DataTemplate> |
| 132 | + </GroupStyle.HeaderTemplate> |
| 133 | + </GroupStyle> |
| 134 | + </ComboBox.GroupStyle> |
| 135 | +</ComboBox> |
| 136 | +``` |
0 commit comments