• Avalonia为DataGrid添加行点击事件


    1.前置参考: Avalonia常用小控件DataGrid

    2.DataGrid.axaml.cs

    1.为DataGridRow添加点击事件 

    PointerPressedEvent.AddClassHandler会让你的程序中所有页面的DataGridRow都来执行这个方法

    PointerPressedEvent.AddClassHandler((x, e) => DataGridRowPointerPressed(x, e), handledEventsToo: true);

    构造行数中添加  dg.LoadingRow += LoadingRow;

    1. private void LoadingRow(object? sender, DataGridRowEventArgs e)
    2. {
    3. DataGridRow dataGridRow = e.Row;
    4. dataGridRow.AddHandler(DataGridRow.PointerPressedEvent, (x, e) => DataGridRow_PointerPressed(dataGridRow, e), handledEventsToo: true);
    5. }

    2.你要实现的点击事件

    private void DataGridRowPointerPressed(DataGridRow x, PointerPressedEventArgs e)
    {
       //你的逻辑
    }

    1. public DataGrid()
    2. {
    3. InitializeComponent();
    4. // 会让全局的Datagrid的行点击都执行这个事件
    5. //PointerPressedEvent.AddClassHandler<DataGridRow>((x, e) => DataGridRow_PointerPressed(x, e), handledEventsToo: true);
    6. dg.LoadingRow += LoadingRow;
    7. dg.ItemsSource = AllTargetList;
    8. DataContext = this;
    9. }
    10. private void LoadingRow(object? sender, DataGridRowEventArgs e)
    11. {
    12. DataGridRow dataGridRow = e.Row;
    13. dataGridRow.AddHandler(DataGridRow.PointerPressedEvent, (x, e) => DataGridRow_PointerPressed(dataGridRow, e), handledEventsToo: true);
    14. }

    3. 列表数据初始化  在Loaded 中给列表赋值的话,LoadingRow 会执行两次,直接在构造方法里边赋值,执行一次,不清楚为啥

    1. public DataGrid()
    2. {
    3. InitializeComponent();
    4. DataContext = this;
    5. Loaded += WindowLoaded;
    6. dg.LoadingRow += LoadingRow;
    7. }
    8. private void WindowLoaded(object? sender, RoutedEventArgs e)
    9. {
    10. // 列表数据赋值如果在这个位置赋值LoadingRow就会执行两次
    11. dg.ItemsSource = AllTargetList;
    12. }
    13. private void LoadingRow(object? sender, DataGridRowEventArgs e)
    14. {
    15. Debug.WriteLine(e.Row.GetIndex());
    16. DataGridRow dataGridRow = e.Row;
    17. dataGridRow.AddHandler(DataGridRow.PointerPressedEvent, (x, e) => DataGridRow_PointerPressed(dataGridRow, e), handledEventsToo: true);
    18. }

  • 相关阅读:
    Oracle查询用户所有表的语句
    挑战杯 机器视觉目标检测 - opencv 深度学习
    2019年互联网高频Java面试题指南!互联网升职加薪方案!
    cmake 选择 vs编译器
    【数据结构与算法】泛型的介绍及使用
    Linux常用命令
    SpringCache入门
    SpringBoot数据响应、分层解耦、三层架构
    Sping高级(源码)--- 1.6Aware接口
    ✔ ★【备战实习(面经+项目+算法)】 10.16学习时间表(总计学习时间:5h)
  • 原文地址:https://blog.csdn.net/confused_kitten/article/details/133805026