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; }
}
}当前运行效果
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; }
}
}当前运行效果
tb.GetBindingExpression(TextBlock.TextProperty)来获取到Text属性的绑定表达式
里面可以得到源的属性
原来是单元格对应的值。搜索WPF 命中测试。
每个数据模板都加上这个事件,然后点击任何单元格,可以以当前列的单元格为起始,下面的递增,那你现在的代码就可以实现啊,都能找到student,就可以找到该student在集合中的位置,然后从该位置循环后边的就行了;