【发布时间】:2014-04-24 04:37:19
【问题描述】:
我的数据网格的第一列中有一个按钮,当我单击它时,我试图获取该行第三列的单元格值,我单击了该按钮。因此,例如,我单击数据网格第 3 行上的按钮,我想要第 3 列第 3 行的单元格值,它是一个 int。我该怎么做?
按钮的 XAML:
<Control:DataGrid.Columns>
<Control:DataGridTemplateColumn>
<Control:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="ShowHideDetailsClick" Foreground="Black">+</Button>
</DataTemplate>
</Control:DataGridTemplateColumn.CellTemplate>
</Control:DataGridTemplateColumn>
<Control:DataGrid.Columns>
C# 处理点击:
private void ShowHideDetailsClick(object sender, RoutedEventArgs e)
{
//want to get cell value here
System.Windows.Controls.Button expandCollapseButton = (System.Windows.Controls.Button)sender;
DependencyObject obj = (DependencyObject)e.OriginalSource;
while (!(obj is ExtendedGrid.Microsoft.Windows.Controls.DataGridRow) && obj != null) obj = VisualTreeHelper.GetParent(obj);
if (obj is ExtendedGrid.Microsoft.Windows.Controls.DataGridRow)
{
if (null != expandCollapseButton && "+" == expandCollapseButton.Content.ToString())
{
(obj as ExtendedGrid.Microsoft.Windows.Controls.DataGridRow).DetailsVisibility = Visibility.Visible;
expandCollapseButton.Content = "-";
}
else
{
(obj as ExtendedGrid.Microsoft.Windows.Controls.DataGridRow).DetailsVisibility = Visibility.Collapsed;
expandCollapseButton.Content = "+";
}
}
}
我使用 DataTable 用从数据库中检索到的数据填充我的数据网格,请参见下面的代码:
public DataTable SourceTable
{
get
{
return _sourceTable;
}
set
{
_sourceTable = value;
OnPropertyChanged("SourceTable");
}
}
SourceTable = new DataTable();
SourceTable.Columns.AddRange(new DataColumn[]{
new DataColumn("InputID", typeof(int)),
new DataColumn("TraderID", typeof(string)),
new DataColumn("TradeDate", typeof(DateTime)),
new DataColumn("TradeTime", typeof(TimeSpan)),
new DataColumn("ClientName", typeof(string)),
new DataColumn("CurPair", typeof(string)),
new DataColumn("Amnt", typeof(int)),
new DataColumn("Action", typeof(string)),
new DataColumn("ExecutedRate", typeof(decimal))
});
DataRow rowSource = null;
var OpenTradesQuery = from qa in connection.QuickAnalyzerInputs
where qa.TradeClosedDateTime == null
select new
{
qa.InputID,
qa.TraderID,
qa.ClientTradedDate,
qa.ClientTradedTime,
qa.ClientName,
qa.CurrencyPair,
qa.TradedAmount,
qa.Action,
qa.ExecutedRate
};
foreach (var rowObj in OpenTradesQuery)
{
rowSource = SourceTable.NewRow();
SourceTable.Rows.Add(rowObj.InputID, rowObj.TraderID, rowObj.ClientTradedDate, rowObj.ClientTradedTime, rowObj.ClientName, rowObj.CurrencyPair, rowObj.TradedAmount, rowObj.Action, rowObj.ExecutedRate);
}
然后在我的 XAML 中,数据网格正在绑定 SourceTable
【问题讨论】: