得到DataGrid的某一行列的值

转载自[孟子E章]

http://blog.csdn.net/net_lover/archive/2008/11/26/3379002.aspx

 

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="SilverlightApplication8.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="600" Height="Auto">
    <Grid x:Name="LayoutRoot" Background="White">
        <ScrollViewer VerticalScrollBarVisibility="Auto" BorderThickness="0">
            <StackPanel Margin="10,10,10,10">
                <data:DataGrid x:Name="dataGrid1"
      Height="200" Margin="0,5,0,10"
      AutoGenerateColumns="False" LoadingRow="dataGrid1_LoadingRow">
                    <data:DataGrid.Columns>
                        <data:DataGridTextColumn  Header="序号" Width="120"  Binding="{Binding IndexName}" />
                        <data:DataGridTextColumn  Header="姓名" Width="120"  Binding="{Binding UserName}" />
                        <data:DataGridTextColumn  Header="地址" Width="120"  Binding="{Binding Address}" />
                        <data:DataGridTemplateColumn Header="功能按钮"  Width="80">
                            <data:DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Content="初始化内容" x:Name="Button1" Click="Button1_Click"  Tag=""></Button>
                                    </StackPanel>
                                </DataTemplate>
                            </data:DataGridTemplateColumn.CellTemplate>
                        </data:DataGridTemplateColumn>
                    </data:DataGrid.Columns>
                </data:DataGrid>
                <TextBlock x:Name="msg"></TextBlock>
            </StackPanel>
        </ScrollViewer>

    </Grid>
</UserControl>

 

 

 

 

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 SilverlightApplication8
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            dataGrid1.ItemsSource = Customer.GetSampleCustomerList();

        }

        private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            Customer bindData = (Customer)e.Row.DataContext;
            Button btn = dataGrid1.Columns[3].GetCellContent(e.Row).FindName("Button1") as Button;
            btn.Content = bindData.UserName;
            btn.Tag = bindData.IndexName + "," + e.Row.GetIndex();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            string[] t = b.Tag.ToString().Split(',');
            msg.Text = "你选择的值是:" + t[0] + " 是 DataGrid 的第 " + t[1] + " 行";
        }
    }
    /// <summary>
    /// 数据对象
    /// </summary>
    public class Customer
    {
        public Int32 IndexName { get; set; }
        public String UserName { get; set; }
        public String Address { get; set; }

        public Customer(Int32 indexName, String userName, String address)
        {
            this.IndexName = indexName;
            this.UserName = userName;
            this.Address = address;
        }

        public static List<Customer> GetSampleCustomerList()
        {
            //示例数据
            List<Customer> data = new List<Customer>();
            for (int i = 0; i < 10; i++)
            {
                data.Add(new Customer(i * i, "孟宪会之" + i.ToString(), "地址之" + i.ToString()));
            }
            return data;
        }
    }
}

 

 

你可能感兴趣的:(datagrid)