Rick
Rick

Reputation: 1274

How to display data in a textblock from a listbox of objects using databinding?

This is my 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>

This is my Person class:

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));
            }

        }

    }

Main:

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

I want that the details are shown in the textblocks next to the listbox using the name that is selected in the listbox. How can I do this using databinding?

Upvotes: 0

Views: 63

Answers (1)

dkozl
dkozl

Reputation: 33364

You can bind to TextBlock to a property of ListBox.SelectedItem. Give ListBox some name and use it ElementName binding

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

Upvotes: 1

Related Questions