你们这上传附件总失败,没办法发工程啊
.xaml代码
- <UserControl x:Class="demo.MainPage"
- 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/markup-compatibility/2006"
- mc:Ignorable="d"
- xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
- d:DesignHeight="300" d:DesignWidth="400">
- <Grid x:Name="LayoutRoot" Background="White">
- <sdk:DataGrid x:Name="dgUserList" AutoGenerateColumns="False" Width="300" >
- <sdk:DataGrid.Columns>
- <sdk:DataGridTextColumn Binding="{Binding id}" Header="ID" Width="*" />
- <sdk:DataGridTextColumn Binding="{Binding name}" Header="NAME" Width="*"/>
- <sdk:DataGridTextColumn Binding="{Binding sex}" Header="SEX" Width="*"/>
- </sdk:DataGrid.Columns>
- </sdk:DataGrid>
- </Grid>
- </UserControl>
复制代码
.cs代码
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- namespace demo
- {
- public partial class MainPage : UserControl
- {
- public MainPage()
- {
- InitializeComponent();
- this.dgUserList.LoadingRow += this.DgUserList_LoadingRow;
- List<UserInfo> userList = new List<UserInfo>();
- UserInfo userInfo = null;
- for(int i = 0; i < 4; i++)
- {
- userInfo = new UserInfo();
- userInfo.id = i + string.Empty;
- userInfo.name = i + string.Empty;
- userInfo.sex = i + string.Empty;
- userList.Add(userInfo);
- }
- this.dgUserList.ItemsSource = userList;
- }
- private void DgUserList_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
- {
- e.Row.MouseLeftButtonUp += this.Row_MouseLeftButtonUp;
- }
- private void Row_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
- {
- MessageBox.Show("id=========" + (this.dgUserList.SelectedItem as UserInfo).id);
- }
- public class UserInfo
- {
- public String id { get; set; }
- public String name { get; set; }
- public String sex { get; set; }
- }
- }
- }
复制代码 |