user3428422
user3428422

Reputation: 4560

WPF Combobox - The object name being set as well as the value

Morning,

I apologise in advance if this is a duplicate question, I am not really sure what is wrong so I don't really know what to search!

However, I have set a Combobox in XAML as

  <ComboBox HorizontalAlignment="Left" 
    Width="120"
    Text="{Binding PV.Loc}"
    SelectedItem="{Binding PV.Loc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
     <ComboBoxItem>.</ComboBoxItem>
     <ComboBoxItem Content="{Binding GetVer}"/>
  </ComboBox>

When I go to select the GetVer value (The property is a string) I get the following:

System.Windows.Controls.ComboBoxItem: The GetVer value

However, when I choose the dot, it sets correctly.

Sure its an easy one, what do I need to change?

Thanks

Upvotes: 0

Views: 1117

Answers (3)

flo_badea
flo_badea

Reputation: 784

did you try this while data-binding? you don't need ComboBoxItems...

//model
class DummyModel{
    public int Id{get;set;}
    public string Value{get;set;}
}
//view-model
class DemoVM:INotifyPropertyChanged{
    private int selId;
    //you can build the list any way you want (even with hardcoded values...)
    public List<DummyModel> Items{get{return new List<DummyModel>();}}
    public int SelectedId{
        get{return selId;}
        set{
            if(selId==value)return;
            selId=value;
            RaisePropertyChanged("SelectedId");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string name){
        //raise event here
    }
}
//view
<ComboBox ItemsSource="{Binding Items}" 
    SelectedValue="{Binding SelectedId}"
    SelectedValuePath="Id" IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False"
    IsEditable="True" TextSearch.TextPath="Value">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222552

You need to set the DisplayMemberPath Property

 <ComboBox HorizontalAlignment="Left" DisplayMemberPath="value"   />
 </ComboBox>

Also you should not use <ComboBoxItem>.</ComboBoxItem> if you are binding data.

Upvotes: 1

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

Set DisplayMemberPath property of Combo.

Upvotes: 0

Related Questions