6

I want to bind a ComboBox item to a string, but it does not work. My code is below.

Code in view:

<ComboBox          
 SelectedValuePath="content" 
 SelectedItem="{Binding ProductName}" 
            ......                       
 <ComboBoxItem>1111111111</ComboBoxItem>
 <ComboBoxItem>2222222222222</ComboBoxItem>
 <ComboBoxItem>333333333333</ComboBoxItem>
</ComboBox>

Code in view model:

private string _productName;
public string ProductName
{
    get { return _productName; }
    set
    {
        if (_productName != value)
        {
            _productName = value;
            RaisePropertyChangedEvent("ProductName");
        }
    } 
}
2
  • 1
    Your code works just fine, you didn't forget to set the DataContext, did you? Commented Sep 10, 2015 at 8:23
  • Yes I did set up DataContext. The string to string binding works fine, but this Combobox binding does not work. Commented Sep 10, 2015 at 8:30

4 Answers 4

5

I assume you want to get the text from the ComboboxItem and not the ComboBoxItem iteself.

So you are binding the wrong information. This should work.

<ComboBox          
SelectedValuePath="content" 
Text="{Binding ProductName}" 
            ......                       
<ComboBoxItem>1111111111</ComboBoxItem>
<ComboBoxItem>2222222222222</ComboBoxItem>
<ComboBoxItem>333333333333</ComboBoxItem>
</ComboBox>
Sign up to request clarification or add additional context in comments.

Comments

1

Selected Item is of type ComboBoxItem, it will not accept String. If you want to display product name in some other place try maybe something like this:

 <TextBox Text="{Binding ElementName=my_ComboBox, Path=SelectedItem}"/>

Comments

1

Just a suggestion. You already use a binding for the SelectedItem, why don't you set up another binding for the Items using the ItemsSource? So you would not need to add them statically in your view.

In addition you would not have the trouble to wonder whether you deal with instances of ComboxItem or String with your SelectedItem binding. In case of the binding via ItemsSource you can be sure that the SelectedItem is a string.

Here is the code:

<ComboBox          
 SelectedValuePath="content" 
 SelectedItem="{Binding ProductName}"
 ItemsSource="{Binding ProductNames}"
</ComboBox>

In your view model (or code behind) you define the ProductNames:

public String[] ProductNames
{
    get
    {
        return _productNames;
    }
    set
    {
        if (_productNames!= value)
        {
            _productNames = value;
            RaisePropertyChangedEvent("ProductNames");
        }
    }
}
String[] _productNames;


public NameOfConstructor()
{
    List<String> productNames = new List<String>();
    productNames.Add("A");
    productNames.Add("B");
    productNames.Add("C");

    ProductNames = productNames.ToArray();
}

If it was possible that the list of names changes during execution, I would use a ObservableCollection<string> instead String[].

Comments

0

It should be like this

  1. Create an observable collection of Product in View Model. Lets Say ProductCollection
  2. Bind to the ComboBox ItemSource as given below
<ComboBox  Name="productComboBox"  Width="200" Height="30" ItemsSource="{Binding ProductCollection}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=ProductName}"></TextBlock>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

if you want to show in textbox somewhere use this

<TextBox Text="{Binding ElementName=productComboBox, Path=SelectedItem}"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.