xaml<Window x:Class="WpfApp5.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/up-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp5"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
        <Window.Resources>
            <DataTemplate x:Key="dtName">
                <TextBlock Text="{Binding Name}"></TextBlock>
            </DataTemplate>
            <DataTemplate x:Key="dtAge">
                <TextBlock Text="{Binding Age}" MouseUp="TextBlock_MouseUp"></TextBlock>
            </DataTemplate>
            <DataTemplate x:Key="dtWeight">
                <TextBlock Text="{Binding Weight}"></TextBlock>
            </DataTemplate>
        </Window.Resources>
    <ListView x:Name="lv">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="姓名" Width="200" CellTemplate="{StaticResource dtName}"></GridViewColumn>
                <GridViewColumn Header="年龄" Width="200" CellTemplate="{StaticResource dtAge}"></GridViewColumn>
                <GridViewColumn Header="重量" Width="200" CellTemplate="{StaticResource dtWeight}"></GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView></Window>xaml.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfApp5
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<Student> students = new List<Student>()
            {
                new Student{Name="张三",Age=23,Weight="1"},
                new Student{Name="李四",Age=33,Weight="2"},
                new Student{Name="王五",Age=13,Weight="3"},
                new Student{Name="张三",Age=23,Weight="4"},
                new Student{Name="李四",Age=33,Weight="5"},
                new Student{Name="王五",Age=13,Weight="6"},
                new Student{Name="张三",Age=23,Weight="7"},
                new Student{Name="李四",Age=33,Weight="2"},
                new Student{Name="王五",Age=13,Weight="3"},
                new Student{Name="张三",Age=23,Weight="4"},
                new Student{Name="李四",Age=33,Weight="5"},
                new Student{Name="王五",Age=13,Weight="6"},
                new Student{Name="张三",Age=23,Weight="7"},
                new Student{Name="李四",Age=33,Weight="8"},
                new Student{Name="王五",Age=13,Weight="9"}
            };
            lv.ItemsSource = students;
        }        private void TextBlock_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //写什么能获得我这一列的属性,比如我想实现在Age列和Weight列可以在点击的元素下方实现顺序加1,
            //因为我有很多这种列,所以我不获取到类的属性,就需要每个数据模板加一个内容上重复的函数,很不爽            //下面这部分是获得实例的,也就是可以获得当前的Student实例,但是不知道点击的哪一个属性
            TextBlock tb = sender as TextBlock;
            Student student = tb.DataContext as Student;
            MessageBox.Show(student.Age.ToString());
        }
    }
    class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Weight { get; set; }
    }
}当前运行效果

解决方案 »

  1.   

    没有直接的方法提供的,不过可以
    tb.GetBindingExpression(TextBlock.TextProperty)来获取到Text属性的绑定表达式
    里面可以得到源的属性
      

  2.   

    取SelectedItem属性即可。
      

  3.   

    咋取,详细点兄弟,现在能取到的只是Student的一个实例
      

  4.   

    咋取,详细点兄弟,现在能取到的只是Student的一个实例
      

  5.   

    咋取,详细点兄弟,现在能取到的只是Student的一个实例
      

  6.   

    你的代码就是点击Age列的某个单元格;你可以试试点击Name单元格就不会触发你写的这个事件;不要被界面效果蒙蔽了眼;
      

  7.   

    咋取,详细点兄弟,现在能取到的只是Student的一个实例
    原来是单元格对应的值。搜索WPF 命中测试
      

  8.   

    这我知道,只要在数据模板把另外两个加上这个事件就行,调用的方法一样,我只是测试所以只写了一个所以你具体需要什么功能?
    每个数据模板都加上这个事件,然后点击任何单元格,可以以当前列的单元格为起始,下面的递增,那你现在的代码就可以实现啊,都能找到student,就可以找到该student在集合中的位置,然后从该位置循环后边的就行了;