<ComboBox Name="ComboBox1">
...
<ComboBox />
<Label Text="{Binding ElementName=ComboBox1, Path=SelectedItem}" />
- РЕДАКТИРОВАТЬ -
Расширенный пример :
<StackPanel>
<ComboBox Name="ComboBox1"
DisplayMemberPath="FirstName"></ComboBox>
<StackPanel DataContext="{Binding ElementName=ComboBox1, Path=SelectedValue}">
<Label Content="{Binding FirstName}" />
<Label Content="{Binding LastName}" />
<Label Content="{Binding Age}" />
</StackPanel>
</StackPanel>
Код:
InitializeComponent();
ObservableCollection<Person> persons = new ObservableCollection<Person>() {
new Person(){ FirstName = "John", LastName = "Doe", Age = 25 },
new Person(){ FirstName = "John", LastName = "Smith", Age = 35 },
new Person(){ FirstName = "Susan", LastName = "Smith", Age = 31 },
new Person(){ FirstName = "Anthony", LastName = "Jones", Age = 31 },
};
ComboBox1.ItemsSource = persons;
Класс:
public class Person
{
public String FirstName { get; set; }
public String LastName { get; set; }
public Int32 Age { get; set; }
}
- РЕДАКТИРОВАТЬ: 2 -
Создать новый класс:
class GroupInfo
{
public String GroupName { get; set; }
public String IP { get; set; }
}
и изменить свой код следующим образом:
foreach (string groupName in _dicPortStatus.Keys)
{
if (!cmbGroups.Items.Contains(groupName))
{
cmbGroups.Items.Add(new GroupInfo(){ GroupName = groupName, IP = <Write Code to get IP>);
cmbGroups.SelectedItem = groupName;
}
}
Измените XAML на следующее:
<ComboBox Name="ComboBox1" DisplayMemberPath="GroupName">
...
<ComboBox />
<Label Text="{Binding ElementName=ComboBox1, Path=SelectedItem.IP}" />