[英]Disable the row selection on right click in wpf datagrid if row is already selected
[英]DataGrid Row Selection on Right Click Event Not Selecting Row
我有一個帶有ContextMenu
的DataGrid
。 我想要的是在彈出上下文菜單(通過右鍵單擊)時,我想要獲取所選行上的列數據。 此數據將用於確認是否應啟用某些上下文菜單選項。
所以我嘗試了MouseRightButtonUp
事件處理程序,但最終得到了NullReferenceException
。
<DataGrid MouseRightButtonUp="DataGrid_MouseRightButtonUp">
private void DataGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e) {
MessageBox.Show(((DataRowView)DataGrid.SelectedItem).Row.ItemArray[0].ToString());
}
然后,我嘗試了SelectionChanged
事件,該事件最終起作用了,但是如果連續多次SelectionChanged
它,則它不會連續運行多次。 我需要這樣,以便每當右鍵單擊一行時,事件就會觸發並返回列數據。 同樣,不需要單擊鼠標左鍵也會觸發此事件。
目前我有哪些可用選項?
您可以嘗試直接在DataGridRow上設置MouseRightClick事件處理程序,如下所示:
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseRightButtonUp" Handler="YourHandler"/>
</Style>
</DataGrid.Resources>
這樣,您將不必嘗試查找被單擊的行,從而限制了發生錯誤的可能性。 您將直接有權訪問事件處理程序中的行。
希望有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.