我有一個DataGrid綁定到數(shù)據(jù)庫表的數(shù)據(jù)庫,我需要獲取中選定行的內(nèi)容DataGrid,例如,我想顯示MessageBox選定行的內(nèi)容。范例DataGrid:因此,如果我選擇第二行,MessageBox則必須顯示類似以下內(nèi)容:646 Jim Biology。
3 回答

12345678_0001
TA貢獻1802條經(jīng)驗 獲得超5個贊
您可以使用該SelectedItem屬性獲取當(dāng)前選定的對象,然后將其轉(zhuǎn)換為正確的類型。例如,如果將您DataGrid綁定到Customer對象集合,則可以執(zhí)行以下操作:
Customer customer = (Customer)myDataGrid.SelectedItem;
或者,您可以綁定SelectedItem到源類或ViewModel。
<Grid DataContext="MyViewModel">
<DataGrid ItemsSource="{Binding Path=Customers}"
SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"/>
</Grid>

森林海
TA貢獻2011條經(jīng)驗 獲得超2個贊
如果您使用的是MVVM模式,則可以將SelectedRecord
VM 的屬性與SelectedItem
DataGrid 綁定在一起,這樣您就可以SelectedValue
在VM中始終擁有。否則,您應(yīng)該使用SelectedIndex
DataGrid 的屬性。
- 3 回答
- 0 關(guān)注
- 952 瀏覽
添加回答
舉報
0/150
提交
取消