Gia
Gia

Reputation: 81

How to binding listviewitem?

i want. When I click on the listview, the textblock below will display the text of the selected item?

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <ListView x:Name="lol">
            <ListViewItem>
                <TextBlock Text="One"/>
            </ListViewItem>
            <ListViewItem>
                <TextBlock Text="Two"/>
            </ListViewItem>
            <ListViewItem>
                <TextBlock Text="Three"/>
            </ListViewItem>
        </ListView>
        <TextBlock Text="{Binding Path=Text,ElementName=lol.SelectedItem}"/>
    </StackPanel>
</Window>

Upvotes: 0

Views: 66

Answers (1)

Rekshino
Rekshino

Reputation: 7325

Here is the binding you need:

<TextBlock Text="{Binding Path=SelectedItem.Content.Text, ElementName=lol}"/>

Be aware, that in your case SelectedItem is a ListViewItem and it's content is a TextBlock.

Upvotes: 3

Related Questions