In a WPF app, in MVP app, I have a combo box,for which I display the data fetched from Database. Before the items added to the Combo box, I want to display the default text such as
" -- Select Team --"
so that on pageload it displays and on selecting it the text should be cleared and the items should be displayed.
Selecting data from DB is happening. I need to display the default text until the user selects an item from combo box.
Please guide me
I dont know if it's directly supported but you could overlay the combo with a label and set it to hidden if the selection isn't null.
eg.
Then in the selection changed handler ...
HappyNomad's solution was very good and helped me eventually arrive at this slightly different solution.
Solution
1. Put a label on top of the combobox.
2. Bind the content of the label to to the combobox Text property.
3. Set the opacity of the combobox to zero , Opacity=0.
4. Write default text in the combobox Text property
Not best practice..but works fine...
Code behind
Easiest way is to use CompositeCollection to merge default text and data from database directly in ComboBox e.g.
And in Resources define StaticResource to bind ComboBox options to your DataContext, because direct binding in CollectionContainer doesn't work correctly.
This way you can define your ComboBox options only in xaml e.g.
Set IsEditable=True on the Combobox element. This will display the Text property of the Combobox