David
David

Reputation: 4117

WPF/C# - ´binding list<string> to combobox

I want my combobox item names and values to be taken from my List of course I don't want my view model to hold combobox items list.

I got a list a,b,c,d

public List<String> ComboList { get; set; }

...

ComboList = new List<String>();
ComboList.Add("A");
ComboList.Add("B");
ComboList.Add("C");
ComboList.Add("D");

and my ComboBox

<ComboBox Margin="29,40,0,526" Width="212" Height="35" Grid.Row="1" ItemsSource="{Binding Path=ComboList, Mode=OneTime}" SelectedValuePath="Key" DisplayMemberPath="Value"></ComboBox>

but it gives me a empty ComboBox ...

enter image description here

Upvotes: 3

Views: 7425

Answers (2)

scottheckel
scottheckel

Reputation: 9244

Remove the SelectedValuePath and DisplayMemberPath attributes. They are wrong.

Upvotes: 5

ykatchou
ykatchou

Reputation: 3727

You forget to do that just before the InitializeComponents into the code-behind :

public void MainWindow(){
  this.Datacontext = this;
  InitializeComponent()
}

Moreover you cannot bind the list directly, you better have to give an ObservableCollection. This is an example :

public ObservableCollection<NetworkCard> NetworksCards { get { return m_aCards; } }

private ObservableCollection<NetworkCard> m_aCards = null;
m_aCards = new ObservableCollection<NetworkCard>(oHelper.ListNetworkCards());

Upvotes: 1

Related Questions