1

Im using combo box and I have a list that opened when you choose the arrow,I want to put text on the combo box that you will see it witout opening the combo box, how can I do that?

currently I try with text="name" ,name="name" witout success ,the combo box doesnt display anything as a text. just list from drop down...

like the following

http://msdn.microsoft.com/en-us/library/ms753382%28v=vs.85%29.aspx

in addition how can I change the arrow like to be like in the link

John Jerrby
  • 1,683
  • 7
  • 31
  • 68
  • 1
    Do you mean a watermark or a default item? Watermark is not part of the list and just gives a hint, whereas default item is one among the list. Which one do you want? – Mat J Jan 20 '14 at 17:11

1 Answers1

2

Set the first item in the collection list as the default selection.

There is an example here:

How to show text in combobox when no item selected?

EDIT :

public class MyViewModel
{
    public MyViewModel()
    {
         Items.Add("Select one item");
         Items.Add("Item1");
         Items.Add("Item2");
         Items.Add("Item3");

         SelectedItem = Items[0];
    }

    private List<String> _items;
    public List<String> Items
    {
         get{ return _items; }
         set
         { 
              _items = value; 
              RaisePropertyChanged(() => Items);
         }
    }

    private String> _selectedItem;
    public String SelectedItem
    {
         get{ return _selectedItem; }
         set
         { 
              _selectedItem= value; 
              RaisePropertyChanged(() => SelectedItem);
         }
    }

}

In your xaml file you have to bind to the list of items and to the selected item:

<ComboBox x:Name="myComboBox"
          ItemsSource="{Binding Items}"
          SelectedValue="{Binding SelectedItem}" />

And don't forget to set the DataContext to your view model.

Community
  • 1
  • 1
yoozz
  • 247
  • 2
  • 15
  • Im using MVVM ,can you please provide simple example – John Jerrby Jan 20 '14 at 17:09
  • Just add the text you want to display as the 'default' selection in the binded list. In the constructor of your viewmodel or in the initialize method, just set the SelectedItem of your combobox the element that you want to display. SelectedItem = Items[0]; – yoozz Jan 20 '14 at 17:12
  • Can you please provide an example...currently I have ItemsSource="{StaticResource myStrings}" – John Jerrby Jan 20 '14 at 17:15
  • Thanks you very much for your support!!!!!!,currenly I use property change like this,how should I cange it to adopt your code set { _isDataLoaded = value; OnPropertyChanged("IsData"); } – John Jerrby Jan 20 '14 at 18:26
  • Your code is just fine too with OnPropertyChanged("IsData"); Remember to set the answer as the correct answer if it helped you. – yoozz Jan 20 '14 at 18:36
  • Sure buddy :) just to verify ,did you mean to use it like OnPropertyChanged("Items"); ? – John Jerrby Jan 20 '14 at 18:58
  • Yes. RaisePropertyChanged() is part of MVVMLight and I am used to use MVVMLight – yoozz Jan 20 '14 at 19:07