Robin's Blog

记录 积累 学习 成长

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

通过Tag属性可以设置自定义的数据:

(1)借助LoadingRow事件,在初始化DataGrid过程中 使用GetCellContent方法,它可以到单元格内的控件。这时,你也就可以得到你点击的是哪一行了,你可以选择将适当的信息赋给该行的Button的Tag属性; 然后在单击button 按钮时,就可以对该信息进行读取和使用了:

(2)如果你不关心在某一行,而很需要得到改行绑定的实体。那么,你可以在定义Button的时候,将Button的Tag属性直接进行空绑定,可以将该该行绑定的实体对象赋值给Tag,那么在单击Button时,就可以直接使用该对象了。

这样,点击的操作就很灵活了,也可以解决很多问题了。

XAML 脚本:

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"   x:Class="SilverlightApplication1.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="1" BorderBrush="Gray">
            
<StackPanel Margin="10">
                
<data:DataGrid x:Name="gridDataList" AutoGenerateColumns="False" LoadingRow="DataGrid_LoadingRow" Height="200" Margin="0 5 0 10">
                    
<data:DataGrid.Columns>
                        
<data:DataGridTextColumn Header="序号" Width="80" Binding="{Binding Index}"/>
                        
<data:DataGridTextColumn Header="姓名" Width="100" Binding="{Binding Name}"/>
                        
<data:DataGridTextColumn Header="地址" Width="260" Binding="{Binding Address}"/>
                        
                        
<data:DataGridTemplateColumn Header="功能按钮" Width="120">
                            
<data:DataGridTemplateColumn.CellTemplate>
                                
<DataTemplate>
                                    
<Button x:Name="Button" Width="50" Content="初始化内容"   Height="21" Tag="{Binding}" Click="Button_Click"></Button>
                                
</DataTemplate>
                            
</data:DataGridTemplateColumn.CellTemplate>
                        
</data:DataGridTemplateColumn>
                    
</data:DataGrid.Columns>
                
</data:DataGrid>

                
<TextBlock x:Name="msg"></TextBlock>

            
</StackPanel>
        
</ScrollViewer>
    
</Grid>
</UserControl>

 

后置代码:

public partial class Page : UserControl
     {
         public Page()
         {
             InitializeComponent();
             gridDataList.ItemsSource = Customer.GetSampleCustomerList();
         }

         private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
         {
             Customer bindData = (Customer)e.Row.DataContext;
             Button btn = gridDataList.Columns[3].GetCellContent(e.Row).FindName("Button") as Button;
             btn.Content = bindData.Name;
             //btn.Tag = bindData.Index + "," + e.Row.GetIndex();
         }

         private void Button_Click(object sender, RoutedEventArgs e)
         {
             Button b = sender as Button;
             Customer c = b.Tag as Customer;

             msg.Text = "你选择" + c.Name + "的Index是:" + c.Index + ",它位于 DataGrid 的第 " + c.Index + " 行";
             //string[] t = b.Tag.ToString().Split(',');
             //msg.Text = "你选择的值是:" + t[0] + " 是 DataGrid 的第 " + t[1] + " 行";
         }
     }

       ///
<summary>
   /// 数据对象
   ///
</summary>
     public class Customer
     {
         public Int32 Index { get; set; }
         public String Name { get; set; }
         public String Address { get; set; }

         public Customer(Int32 indexName, String userName, String address)
         {
             this.Index = indexName;
             this.Name = 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, "Jack 之" + i.ToString(), "地址之" + i.ToString()));
             }
             return data;
         }
     }
posted on 2010-01-07 10:50  Robin99  阅读(148)  评论(0编辑  收藏  举报