1

I need to show a default text in my ComboBox, this text must not changed also when the user select an item of the Combobox, actually for do this I've created this structure:

<ComboBox ItemsSource="{Binding AvailableNations}" Width="160" Height="55" Margin="0, 0, 0, 15" 
           Text="Select Countries" IsEditable="True">
     <ComboBox.ItemTemplate>
         <DataTemplate>
             <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Item.Name}" />
         </DataTemplate>
     </ComboBox.ItemTemplate>
</ComboBox>

this display as default text Select Countries but if I select an item the default text will disappear and the item selected will be displayed, how can I fix this?

Funk
  • 10,976
  • 1
  • 17
  • 33
AgainMe
  • 760
  • 4
  • 13
  • 33
  • `ComboBox` is intended to display selected item. Why don't you show `"Select country:"` next to it using `TextBlock`? Why it has to be a part of `ComboBox`? Normally this behavior (to display hint) is used when `ComboBox` doesn't have any selection. But as soon as it have one the hint is not needed anymore and selected value is displayed. – Sinatr Sep 08 '16 at 11:57
  • 'cause I need to display a default text in combobox not the selected item, the user could select more items in my combobox.. – AgainMe Sep 08 '16 at 11:57
  • You can set `Text` to `"Select Countries"` after selection is processed (I assume what you display that multi-selection to the user somehow, having to open dropdown to see it doesn't sounds like a good design). Have you tried to look for existing multi-selection `ComboBox`es? E.g. [here](http://stackoverflow.com/q/508506/1997232). – Sinatr Sep 08 '16 at 12:05
  • I wrote my own class to manage multi selection – AgainMe Sep 08 '16 at 12:06
  • Am I correct in my understanding that you want the text to display `Select Countries :` when blank, and something like `Select Countries : CAN, US, ETC` when item(s) are selected? – Rachel Sep 08 '16 at 15:31
  • @Rachel nope, the value displayed should be even `Select Countries`. I've a list of checkboxes inside the combobox – AgainMe Sep 08 '16 at 15:34

2 Answers2

2

You could use a Combined Template (ref post)

<Window.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="NormalItemTemplate" >
            <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Item.Name}" />
        </DataTemplate>
        <DataTemplate x:Key="SelectionBoxTemplate" >
            <TextBlock>Select Countries</TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="CombinedTemplate">
            <ContentPresenter x:Name="Presenter"
                   Content="{Binding}"
                   ContentTemplate="{StaticResource NormalItemTemplate}" />
            <DataTemplate.Triggers>
                <DataTrigger
                        Binding="{Binding RelativeSource={RelativeSource FindAncestor,ComboBoxItem,1}}"
                        Value="{x:Null}">
                    <Setter TargetName="Presenter" Property="ContentTemplate"
                            Value="{StaticResource SelectionBoxTemplate}" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ComboBox ItemsSource="{Binding AvailableNations}"                  
              SelectedItem="{Binding SelectedNation}"
              ItemTemplate="{StaticResource CombinedTemplate}"
              Width="160" Height="55" Margin="0, 0, 0, 15" >
    </ComboBox>
</Grid>

The way it works is described in the original answer. Note that the suggested solution will only work when IsEditable is set to false, I assume that won't be a problem in your case. Second, to get the text displayed at start up I bound SelectedItem (e.g. to the first item in the collection).

Community
  • 1
  • 1
Funk
  • 10,976
  • 1
  • 17
  • 33
2

From the comments, it sounds like you just want the Select Countries text to display at all times, even when an item is selected.

Personally I would just go the simple route and place a TextBox on top of the ComboBox, and hide the Display Text of the ComboBox by using a Transparent Foreground color.

Here's a quick example demonstrating it :

<Grid>
    <ComboBox SelectedIndex="1" Foreground="Transparent">
        <ComboBox.Resources>
            <Style TargetType="{x:Type ComboBoxItem}">
                <!-- Make sure ComboBoxItems don't have transparent text -->
                <Setter Property="Foreground" Value="{StaticResource {x:Static SystemColors.ControlTextBrushKey}}" />
            </Style>
        </ComboBox.Resources>
        <ComboBoxItem>Test 1</ComboBoxItem>
        <ComboBoxItem>Test 2</ComboBoxItem>
        <ComboBoxItem>Test 3</ComboBoxItem>
    </ComboBox>

    <TextBlock Text="Select Countries" Margin="4,3" IsHitTestVisible="False" />
</Grid>

And the results (note that SelectedIndex = 1)

enter image description here

I'm sure there's other ways too, such as overwriting the way it paints the display text, or changing the control template, but this seemed like the easiest to me.

Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490