WPF中DataGrid如何单击一行,触发一个事件,本人小菜,请详解,写代码
博客园我看了好多,不过都不太懂,没有找到ItemDataBound这个事件,最好XAML和.cs的代码都有,谢谢...
博客园我看了好多,不过都不太懂,没有找到ItemDataBound这个事件,最好XAML和.cs的代码都有,谢谢
展开
展开全部
using System.Data;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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)
{
// 选择行,这个事件会被激活
}
}
//
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="dg" SelectionChanged="dg_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询