1

What I'm wanting to do is display the selected listview item's name 'if an item is selected' next to the 'Selected' label. I'm not entirely sure how to go about binding that, hope someone can help. Thank you.

enter image description here

XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="240" Width="230">
    <Grid>
        <ListView Margin="10,10,10,43" Name="lvDataBinding">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="Name: " />
                        <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                        <TextBlock Text=", " />
                        <TextBlock Text="Age: " />
                        <TextBlock Text="{Binding Age}" FontWeight="Bold" />
                        <TextBlock Text=" (" />
                        <TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                        <TextBlock Text=")" />
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Label Content="Selected:" HorizontalAlignment="Left" Margin="10,172,0,0"/>
        <Label Content="" HorizontalAlignment="Left" Margin="72,172,0,0" Width="140"/>
    </Grid>
</Window>

CS

using System.Collections.Generic;
using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<User> items = new List<User>();
            items.Add(new User() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com" });
            items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
            items.Add(new User() { Name = "Sammy Doe", Age = 13, Mail = "sammy.doe@gmail.com" });
            lvDataBinding.ItemsSource = items;
        }
    }

    public class User
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Mail { get; set; }
    }
}
JokerMartini
  • 5,674
  • 9
  • 83
  • 193
  • See this [question](http://stackoverflow.com/questions/4529242/how-do-i-bind-a-listview-selecteditem-to-a-textbox-using-the-twoway-mode) – Muaddib Oct 06 '15 at 12:39

1 Answers1

1

Try this:

<Label Content="{Binding SelectedItem.Name, ElementName=lvDataBinding}" .../>

Or:

<Label Content="{Binding ElementName=lvDataBinding, Path=SelectedItem.Name}" .../>
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109