2015-10-06 67 views
1

我想要做的是在'Selected'标签旁边显示选中的列表视图项的名称'如果选择了某个项'。我不完全确定如何去约束,希望有人可以帮忙。 谢谢。选中的列表视图项显示在标签中

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 = "[email protected]" }); 
      items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "[email protected]" }); 
      items.Add(new User() { Name = "Sammy Doe", Age = 13, Mail = "[email protected]" }); 
      lvDataBinding.ItemsSource = items; 
     } 
    } 

    public class User 
    { 
     public string Name { get; set; } 
     public int Age { get; set; } 
     public string Mail { get; set; } 
    } 
} 
+0

看到这个[问题](http://stackoverflow.com/questions/4529242/how-do-i-bind-a-listview-selecteditem-to-a-textbox-using-所述-双向模式) – Muaddib

回答

1

试试这个:

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

或者:

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