I
I
Ingvar Von Bjork2016-10-06 19:28:26
C++ / C#
Ingvar Von Bjork, 2016-10-06 19:28:26

How to add text to combobox that will not be displayed when dropped?

The essence is this: there is a combobox in which strings are written and the property DropDownStyle: DropDownList is added. It is necessary that while the combobox is not active it displays the inscription "Select from the list". Question: how to implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rou1997, 2016-10-06
@Rou1997

He has a property Text.

S
Sanostee, 2016-10-06
@andrewpianykh

If the question is about WPF, then
Method 1 (for people) - Redefine the Template for the ComboBox as we need, there are many options, in the example below, using the trigger, we display a TextBlock (SelTextBlock) with the text "Select from the list" if SelectedItem == null:

<Style TargetType="ComboBoxItem">
    ...
</Style>

<Style x:Key="ComboBoxToggleButtonStyle" TargetType="ToggleButton">
  ...
</Style>

<Style x:Key="ComboBoxEditableTextBoxStyle" TargetType="TextBox">
  ...
</Style>

<Style TargetType="ComboBox">
  <Setter Property="SnapsToDevicePixels" Value="True" />
  <Setter Property="OverridesDefaultStyle" Value="True" />
  ...
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="ComboBox">
        <Grid>
          <ToggleButton x:Name="btn"
                  ClickMode="Press"
                  Focusable="false"
                  IsChecked="{Binding IsDropDownOpen,
                            Mode=TwoWay,
                            RelativeSource={RelativeSource TemplatedParent}}"
                  Style="{StaticResource ComboBoxToggleButtonStyle}">
            <Grid>
              <ContentPresenter x:Name="ContentSite"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Center"
                        Content="{TemplateBinding SelectionBoxItem}"
                        ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" />
              <TextBlock x:Name="SelTextBlock"
                     Text="Выберите из списка"
                     Visibility="Hidden" />
              <TextBox x:Name="PART_EditableTextBox"
                   IsReadOnly="{TemplateBinding IsReadOnly}"
                   Style="{StaticResource ComboBoxEditableTextBoxStyle}"
                   Visibility="Hidden" />
            </Grid>
          </ToggleButton>
          <Popup x:Name="PART_Popup"
               AllowsTransparency="True"
               Focusable="False"
               IsOpen="{Binding IsDropDownOpen,
                      RelativeSource={RelativeSource TemplatedParent}}"
               Placement="Bottom"
               PopupAnimation="Fade"
               SnapsToDevicePixels="True">
            <Border MinWidth="{TemplateBinding ActualWidth}"
                MaxHeight="{TemplateBinding MaxDropDownHeight}"
                Background="White"
                BorderBrush="Lavender"
                BorderThickness="1,0,1,1"
                Padding="0,0,0,4">
              <ScrollViewer CanContentScroll="True"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto">
                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
              </ScrollViewer>
            </Border>
          </Popup>
        </Grid>
        <ControlTemplate.Triggers>
          <Trigger Property="IsEditable" Value="True">
            <Setter TargetName="btn" Property="Tag" Value="Editable" />
            <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible" />
            <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden" />
            <Setter TargetName="SelTextBlock" Property="Visibility" Value="Hidden" />
          </Trigger>
          <Trigger Property="SelectedItem" Value="{x:Null}">
            <Setter TargetName="SelTextBlock" Property="Visibility" Value="Visible" />
          </Trigger>
          ...
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

Method 2 (for perverts) - Overlay a TextBlock with the text "Choose from the list" on top of the ComboBox and using Binding and a custom ObjectToVisibilityConverter render it if SelectedItem == null:
...
<Window.Resources>
  <local:ObjectToVisibilityConverter x:Key="ObjectToVisibilityConverter" />
</Window.Resources>
...
<Grid>
  <TextBlock Margin="4,0,0,0"
         HorizontalAlignment="Left"
         VerticalAlignment="Center"
         Panel.ZIndex="1000"
         Text="Выберите из списка"
         Visibility="{Binding ElementName=CmbBox,
                  Path=SelectedItem,
                  Converter={StaticResource ObjectToVisibilityConverter}}" />
  <ComboBox x:Name="CmbBox" Width="200" />
</Grid>

public class ObjectToVisibilityConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  {
    return value == null ? Visibility.Visible : Visibility.Hidden;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question