Marekus
Marekus

Reputation: 91

WPF binding 2 properties from 1 List

So what i am trying to accomplish is that i am trying to bind 2 properties from 1 list to 2 different ComboBoxes.

code:

combobox1.DataContext = class.repository;
combobox2.DataContext = class.repository;

and in xaml

<ComboBox x:Name="combobox1" ItemsSource="{Binding Name}"/>
<ComboBox x:Name="combobox2" ItemsSource="{Binding Password}"/>

example - repository[0] = "NAME1"

The result i get is when i open ComboBox looks like:

1 item - N

2 item - A

3 item - M and so on..

and result i want is

1 item = NAME1

2 item = NAME2 ...

Thanks for replies.

Upvotes: 0

Views: 167

Answers (2)

mm8
mm8

Reputation: 169330

If repository is a string[], you should bind the ItemsSource to the DataContext itself:

<ComboBox x:Name="combobox1" ItemsSource="{Binding}"/>

If repository is an IEnumerable<YourClass> where YourClass is a type with a Name and a Password property, you should also set the DisplayMemberPath property:

<ComboBox x:Name="combobox1" ItemsSource="{Binding}" DisplayMemberPath="Name" />
<ComboBox x:Name="combobox2" ItemsSource="{Binding}" DisplayMemberPath="Password"/>

Upvotes: 1

Nick
Nick

Reputation: 5042

You should use DisplayMemberPath property of the ComboBox to specify you want to see the value of propery "Name".

Upvotes: 1

Related Questions