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];
何も機能していません。さらに進む方法を教えてください。
c選択変更イベント(SelectionChanged = "ItemsView_OnSelectionChanged")をサブスクライブし、ハンドラーを使用して必要なものをすべて取得します。ビヘイビアー(およびMVVM)でそれを行うか、コードの背後にハンドラーを配置することができます。
ハンドラーのコード例
private void ItemsView_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var dg = sender as DataGrid;
if (dg == null) return;
var index = dg.SelectedIndex;
//here we get the actual row at selected index
DataGridRow row = dg.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow;
//here we get the actual data item behind the selected row
var item = dg.ItemContainerGenerator.ItemFromContainer(row);
}
さらに説明が必要な場合はお知らせください。よろしく。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加