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

时间:2014-12-06 09:19:24

标签: c# wpf xaml data-binding listbox

这是我的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();
        }
    }

我希望使用列表框中选择的名称在列表框旁边的文本块中显示详细信息。 我怎么能用数据绑定做到这一点?

1 个答案:

答案 0 :(得分: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}"/>
相关问题