@H_
404_7@通过Tag
属性可以设置
自定义的数据:
@H_404_7@(1)借助LoadingRow事件,在初始化DataGrid过程中 使用GetCellContent方法,它可以到单元格内的控件。这时,你也就可以得到你点击的是哪一行了,你可以选择将适当的信息赋给该行的Button的Tag属性; 然后在单击button 按钮时,就可以对该信息进行读取和使用了:
@H_
404_7@(2)如果你不关心
在某一行,而很需要得到改行绑定的实体。那么,你可以在定义Button的时候,将Button的Tag
属性直接进行空绑定,可以将该该行绑定的实体对象赋值给Tag,那么在单击Button时,就可以直接使用该对象了。
@H_404_7@这样,点击的操作就很灵活了,也可以解决很多问题了。
@H_
404_7@XAML 脚本:
<
UserControl
xmlns:data
="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
x:Class
="SilverlightApplication1.Page"
xmlns
="http://scheR_906_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://scheR_906_11845@as.microsoft.com/winfx/2006/xaml"
Width
="600"
Height
="Auto"
>
<
Grid
x:Name
="LayoutRoot"
BACkground
="White"
>
<
ScrollViewer
VerticalScrollBarVisibility
="Auto"
BorderThickness
="1"
BorderBrush
="Gray"
>
<
StackPanel
@H_399_29
@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
>
@H_
404_7@
@H_
404_7@后置
代码:
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; } }