Silverlight 2.0 中 DataGrid得到模板列中的按钮,并得到所点击的行号和键值

通过Tag属性可以设置自定义的数据,使用GetCellContent方法可以到单元格内的控件,所以就可以得到你点击的是哪一行了。点击的操作就很容易了。

下面的是全部的源代码

<xmp></xmp>

Page,xaml

  1. <UserControlxmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
  2. x:Class="DataGridSnippets.Page"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. Width="600"Height="Auto">
  6. <ScrollViewerVerticalScrollBarVisibility="Auto"BorderThickness="0">
  7. <StackPanelMargin="10,10,10,10">
  8. <data:DataGridx:Name="dataGrid1"
  9. Height="200"Margin="0,5,0,10"
  10. AutoGenerateColumns="False"LoadingRow="dataGrid1_LoadingRow">
  11. <data:DataGrid.Columns>
  12. <data:DataGridTextColumnHeader="序号"Width="120"Binding="{BindingIndexName}"/>
  13. <data:DataGridTextColumnHeader="姓名"Width="120"Binding="{BindingUserName}"/>
  14. <data:DataGridTextColumnHeader="地址"Width="120"Binding="{BindingAddress}"/>
  15. <data:DataGridTemplateColumnHeader="功能按钮"Width="80">
  16. <data:DataGridTemplateColumn.CellTemplate>
  17. <DataTemplate>
  18. <StackPanelOrientation="Horizontal">
  19. <ButtonContent="初始化内容"x:Name="Button1"Click="Button1_Click"Tag=""></Button>
  20. </StackPanel>
  21. </DataTemplate>
  22. </data:DataGridTemplateColumn.CellTemplate>
  23. </data:DataGridTemplateColumn>
  24. </data:DataGrid.Columns>
  25. </data:DataGrid>
  26. <TextBlockx:Name="msg"></TextBlock>
  27. </StackPanel>
  28. </ScrollViewer>
  29. </UserControl>
Page.xaml.cs

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Windows;
  4. usingSystem.Windows.Controls;
  5. namespaceDataGridSnippets
  6. {
  7. publicpartialclassPage:UserControl
  8. {
  9. publicPage()
  10. {
  11. InitializeComponent();
  12. //绑定数据
  13. dataGrid1.ItemsSource=Customer.GetSampleCustomerList();
  14. }
  15. privatevoiddataGrid1_LoadingRow(objectsender,DataGridRowEventArgse)
  16. {
  17. CustomerbindData=(Customer)e.Row.DataContext;
  18. Buttonbtn=dataGrid1.Columns[3].GetCellContent(e.Row).FindName("Button1")asButton;
  19. btn.Content=bindData.UserName;
  20. btn.Tag=bindData.IndexName+","+e.Row.GetIndex();
  21. }
  22. privatevoidButton1_Click(objectsender,RoutedEventArgse)
  23. {
  24. Buttonb=senderasButton;
  25. string[]t=b.Tag.ToString().Split(',');
  26. msg.Text="你选择的值是:"+t[0]+"是DataGrid的第"+t[1]+"行";
  27. }
  28. }
  29. ///<summary>
  30. ///数据对象
  31. ///</summary>
  32. publicclassCustomer
  33. {
  34. publicInt32IndexName{get;set;}
  35. publicStringUserName{get;set;}
  36. publicStringAddress{get;set;}
  37. publicCustomer(Int32indexName,StringuserName,Stringaddress)
  38. {
  39. this.IndexName=indexName;
  40. this.UserName=userName;
  41. this.Address=address;
  42. }
  43. publicstaticList<Customer>GetSampleCustomerList()
  44. {
  45. //示例数据
  46. List<Customer>data=newList<Customer>();
  47. for(inti=0;i<10;i++)
  48. {
  49. data.Add(newCustomer(i*i,"孟宪会之"+i.ToString(),"地址之"+i.ToString()));
  50. }
  51. returndata;
  52. }
  53. }
  54. }

本文基于Silverlight 2.0正式版。

你可能感兴趣的:(windows,Microsoft,silverlight)