2014-12-06 51 views
0

这是我的XAML:如何使用数据绑定从对象的列表框中显示文本块中的数据?

<Window x:Class="H7_oef1_listBinding.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="517.164" Width="733.955"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="457*"/> 
      <ColumnDefinition Width="60*"/> 
     </Grid.ColumnDefinitions> 
     <ListBox ItemsSource="{Binding}" HorizontalAlignment="Left" Height="299" Margin="10,10,0,0" VerticalAlignment="Top" Width="128"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Padding="5,0,5,0" Text="{Binding Name}" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
     <TextBlock HorizontalAlignment="Left" Margin="295,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Path=Name}"/> 
     <TextBlock HorizontalAlignment="Left" Margin="295,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Path=Street}"/> 

    </Grid> 
</Window> 

这是我的Person类:

class Person : INotifyPropertyChanged 
    { 
     string name; 
     string street; 

     public string Name 
     { 
      get { return name; } 
      set 
      { 
       name = value; 
       OnPropertyChanged(); 
      } 
     } 

     public string Street 
     { 
      get { return street; } 
      set 
      { 
       street = value; 
       OnPropertyChanged(); 
      } 
     } 

     public static ObservableCollection<Person> GetPersons() 
     { 
      var persons = new ObservableCollection<Person>(); 
      persons.Add(new Person() { Name = "name1", Street = "street1", City = "city1", State = "state1", Zip = "1111", Phone = "1111", Cell = "111" }); 
      persons.Add(new Person() { Name = "name2", Street = "street2", City = "city2", State = "state2", Zip = "2222", Phone = "2222", Cell = "2222" }); 
      return persons; 

     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void OnPropertyChanged([CallerMemberName] string caller = "") 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(caller)); 
      } 

     } 

    } 

主营:

public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = Person.GetPersons(); 
     } 
    } 

我想要的细节在的TextBlocks所示的列表框旁边使用在列表框中选择的名称。 如何使用数据绑定来做到这一点?

+0

结合参见[如何:绑定到收藏并展示基于选择信息(http://msdn.microsoft.com/en-us/library/aa970558( v = vs.110).aspx)和[数据绑定概述](http://msdn.microsoft.com/zh-cn/library/ms752347(v = vs.110))中的* Binding to Collections *部分。 aspx)MSDN上的文章。 – Clemens 2014-12-06 09:27:53

回答

1

您可以将TextBlock绑定到ListBox.SelectedItem的属性。给ListBox一些名字,并用它ElementName

<ListBox ItemsSource="{Binding}" ... x:Name="myListBox"> 
    <!-- ... --> 
</ListBox> 
<TextBlock ... Text="{Binding ElementName=myListBox, Path=SelectedItem.Name}"/> 
<TextBlock ... Text="{Binding ElementName=myListBox, Path=SelectedItem.Street}"/> 
+0

谢谢!我找不到这个。 – Rick 2014-12-06 11:07:54

相关问题