John Jerrby
John Jerrby

Reputation: 1703

Combo box default value

I want to use combo box and the following code is working but now I want to add to the combo box header default value ,i.e. there is value and like Item and when you open it you have the option to change it,how I can do that?

<ComboBox  ItemsSource="{Binding Items}" SelectedValue="{Binding SelectedItem}"  
 Name="comboBox1" Text="Item" Grid.Column="3" Grid.Row="2" />

the code

private List<String> _items;
private String _selectedItem;
private String _selectedBusinessItem;
public List<String> Items
{
    get { return _items; }
    set
    {
        _items = value;
        OnPropertyChanged("Items");
    }
}

public String SelectedItem1
{
    get { return _selectedItem; }
    set
    {
        _selectedItem = value;
        OnPropertyChanged("Items");
    }
}

private void InitCombo()
{
    Items = new List<string> { "item",  "Item2", "Item3" };
    SelectedItem1 = Items[0];
}

Upvotes: 1

Views: 1582

Answers (3)

Muthukrishnan R
Muthukrishnan R

Reputation: 159

  1. List Items = new List { "item", "Item 2", "Item 3" };

  2. Set the Selected Index = 0, It will select the first element in the combo box Item-source

XAML:

<ComboBox  ItemsSource="{Binding Items}"  
           Name="comboBox1" Grid.Column="3" Grid.Row="2"
           SelectedIndex="0"/>

Upvotes: 0

deloreyk
deloreyk

Reputation: 1239

It's hard to understand you are asking, but I think you are just looking for the ComboBox to show the first value in the collection of Items.

I believe you can do this a few ways.

First you need to fix SelectedValue binding to match your property name, and drop the Text="Item":

<ComboBox  ItemsSource="{Binding Items}" SelectedValue="{Binding SelectedItem1}"  
           Name="comboBox1" Grid.Column="3" Grid.Row="2" />

From your code you can set the SelectedValue1 property that you have to any of the string items. Examples of this:

SelectedValue1 = "item";

-or-

SelectedValue1 = Items.FirstOrDefault();

I used FirstOrDefault as a safety incase these items didn't exist.

-or-

SelectedValue1 = Items[0];

And there are several more options here. But I'm going to try and limit the scope of the answer.

Also, you should be able to set the ComboBox.SelectedIndex to 0.

<ComboBox  ItemsSource="{Binding Items}" SelectedValue="{Binding SelectedItem1}"  
           Name="comboBox1" Grid.Column="3" Grid.Row="2"
           SelectedIndex="0"/>

Upvotes: 3

Sheridan
Sheridan

Reputation: 69969

I think that you're talking about the ComboBox.Text Property... from the linked page:

Gets or sets the text of the currently selected item

This is not a free field that you can display a message in. It displays the value of the currently selected item from the ComboBox.Items collection. If the text is not in one of the items, then this TextBox 'should' not display that value.

However, there are always workarounds. The correct way to do it would be to define a new ControlTemplate for the ComboBox that contains a TextBlock that is overlayed on top of the selected item TextBox and hidden when required.

Some people think that that is too much work though and so you can find a number of alternative solutions in the How to display default text “--Select Team --” in combo box on pageload in WPF? post here on StackOverflow.

Upvotes: 1

Related Questions