2
votes

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

1
Yes, write a load of templating code, rewrite it as an attached behaviour and then realize users aren't dumb.It'sNotALie.
Watermarks are unnecessary, a bitch to code and really don't add any value to whatever you are creating.Brian

1 Answers

-1
votes

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}"/>