我在datagrid中有一个文本框,数据来自数据库。假设我有10行这些文本框的值。当我点击这一行时,能够获取所选行的索引。我的目标是,如果一旦文本框的值发生改变,我需要检测它是哪一行(哪个值),并根据此值进行一些计算,然后显示同一行的另一个字段。因此,我能够知道被点击的是哪一行。`我使用以下声明的Datagrid:
<dg:DataGrid Name="dgBudgetAllocation" CanUserDeleteRows="False" CanUserAddRows="False" CanUserSortColumns="True"
IsSynchronizedWithCurrentItem="True" MaxHeight="400" RowHeight="70" SelectionUnit="Cell" SelectedValue="" SelectionMode="Single"
AutoGenerateColumns="False" GridLinesVisibility="None" HeadersVisibility="Column" PreviewMouseDown="DgBudgetAllocation_OnPreviewMouseDown" SelectedCellsChanged="DgBudgetAllocation_OnSelectedCellsChanged" MouseDown="DgBudgetAllocation_OnMouseDown" PreviewMouseUp="DgBudgetAllocation_OnPreviewMouseUp" PreviewKeyDown="DgBudgetAllocation_OnPreviewKeyDown" HorizontalAlignment="Left">
<dg:DataGridTemplateColumn Header="Budget Type" SortMemberPath="BUDGETYPE"
MinWidth="50" HeaderStyle="{DynamicResource dgHeaderLeftJust}" CellStyle="{DynamicResource dgColumnRightJust}">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding BUDGETYPE}" HorizontalAlignment="left" VerticalAlignment="Top" Margin="0,0,3,0" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
我尝试了基于不同人的建议的以下代码片段。但是我得到的选择索引是-1。
DataRowView drv = (DataRowView)dgBudgetAllocation.SelectedItem;
object item = dgBudgetAllocation.SelectedItem;
string ID = (dgBudgetAllocation.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
DataGrid row1 = (DataGrid)dgBudgetAllocation.SelectedItems[1];
var row = dgBudgetAllocation.SelectedItems[0];
什么都不起作用了。 请建议我如何进一步操作。