WPF中DataGrid如何单击一行,触发一个事件,本人小菜,请详解,写代码

2024-12-14 00:49:53
推荐回答(1个)
回答1:

using System.Data;
namespace WpfApplication1
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataTable dt = new DataTable();
            dt.Columns.Add("Firstname");
            dt.Columns.Add("Lastname");
            DataRow dr = dt.NewRow();
            dr["Firstname"] = "allen";
            dr["Lastname"] = "coww";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Firstname"] = "allen";
            dr["Lastname"] = "coww";
            dt.Rows.Add(dr);
            dg.ItemsSource = dt.DefaultView;
        }
        private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        // 选择行,这个事件会被激活
        }
    }

//

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">