2

I have a WPF Combobox that is bound to a list of viewModel objects. Initially the SelectedItem is null, and thus the Combobox display is blank.

When the selected item is null, I would prefer the Combobox to display "Select an item" to guide the user to select something from the combobox. sort of like the way, some text boxes contain gray text such as "enter user name"

Any ideas on how to do this?

Edit:

I ended up using the suggestion to overlay a textbox, and change its visibility based on the value of SelecteItem

Community
  • 1
  • 1
Mitch
  • 1,173
  • 1
  • 10
  • 15
  • 1
    Yes, write a load of templating code, rewrite it as an attached behaviour and then realize users aren't dumb. – It'sNotALie. Aug 07 '13 at 20:40
  • Watermarks are unnecessary, a bitch to code and really don't add _any_ value to whatever you are creating. – Brian Aug 07 '13 at 20:42
  • Check out this question: http://stackoverflow.com/questions/1426050/how-to-display-default-text-select-team-in-combo-box-on-pageload-in-wpf or this question: http://stackoverflow.com/questions/8653383/default-text-in-combobox for some options. – Brian S Aug 07 '13 at 20:58

1 Answers1

-1

Try This- Change ItemsSource and SelectedValue according to your code.I just Showed How you can Achieve this..

<ComboBox Height="23" Name="comboBox1" Width="120" ItemsSource="{Binding OCString,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=Window}}" SelectedValue="{Binding Selected,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=Window},UpdateSourceTrigger=PropertyChanged}">
        <ComboBox.Style>
            <Style TargetType="{x:Type ComboBox}">
                <Style.Triggers>
                    <Trigger Property="SelectedIndex" Value="-1">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <ComboBox Text="Select an Item" IsReadOnly="True" IsEditable="True" ItemsSource="{Binding OCString,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=Window}}" SelectedValue="{Binding Selected,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=Window},UpdateSourceTrigger=PropertyChanged}"/>

    </ControlTemplate>
    </Setter.Value>
    </Setter>
                    </Trigger>
                </Style.Triggers>
    </Style>
    </ComboBox.Style>
    </ComboBox>

or Simply->

<ComboBox Text="Select an Item" IsReadOnly="True" IsEditable="True" ItemsSource="{Binding OCString,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType=Window}}" SelectedValue="{Binding Selected,Mode=TwoWay,RelativeSource={RelativeSource AncestorType=Window},UpdateSourceTrigger=PropertyChanged}"/>
Vishal
  • 604
  • 1
  • 12
  • 25