WPF解决方案:
假设视图模型的属性:
public List<ListBoxPersonExample> ListBoxItems { get; set; }
public ListBoxPersonExample SelectedPerson
{
get { return _selectedPerson; }
set { _selectedPerson = value; RaisePropertyChanged("SelectedPerson");}
}
其中类ListBoxPersonExample
定义为:
public class ListBoxPersonExample
{
public ListBoxPersonExample(string name)
{
Name = name;
}
public string Name { get; set; }
public override string ToString()
{
return "My name is :" + Name;
}
}
此 XAML 将满足您的需求:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox Name="ListBoxName" ItemsSource="{Binding ListBoxItems}"
SelectedItem="{Binding SelectedPerson}"></ListBox>
<Label Grid.Column="1" Content="{Binding SelectedPerson.Name}"></Label>
</Grid>