0

I have a ComboBox that is data bound where I cannot insert sentinel items. I would like to show a default string if there is no selected value in the ComboBox.

I already tried using a style for this:

<ComboBox.Style>
    <Style TargetType="ComboBox">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=SelectedIndex}" Value="-1">
                <Setter Property="Text" Value="(unconstrained)" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ComboBox.Style>

but this completely broke data binding (no values were selectable).

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552

1 Answers1

0

You could try this (not really fortunate) solution: You could add another element to your list with value "(unconstrained)" at the first position and set SelectedIndex to 0 in your XAML. Then, when selection changes, you delete this element from the underlying list for ComboBox and move selected index one place up (since the head of the list has been deleted).

This is not elegant at all, but seems like the only solution since ComboBox does not have some kind of Header property and I am not sure what Text does on it (probably it is just inherited).

user1635881
  • 239
  • 3
  • 11
  • Unfortunately as I specified in the question I can't insert sentinel items to do this (the collection I'm binding to is already used in thousands of places and fixing them all to understand that the first item is a dummy value is infeasible) – Billy ONeal Sep 02 '15 at 17:58