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

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

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