• 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. }

  • 相关阅读:
    idea打开hierarchy面板
    风控中策略规则发现的两种模型方法
    Http协议和Https协议
    微信小程序食疗微信小程序的设计与实现
    Graphviz安装教程
    嵌入式培训学费贵不贵?
    GIT 工具使用
    vue源码分析-响应式系统(二)
    【Vue五分钟】 五分钟了解webpack的核心概念
    首次扭亏为盈后,货拉拉还想靠造车更上一层楼?
  • 原文地址:https://blog.csdn.net/confused_kitten/article/details/133805026