2010-03-28 71 views
0

問題是,無論何時網格的行被右鍵單擊,所選的項目都是空的。當任何行右鍵單擊時,我會選擇一個網格的行嗎?在網格行上點擊右鍵

感謝 賈馬爾

+0

Silverlight 4?你在引用DataGrid? – AnthonyWJones 2010-03-28 13:10:17

+0

是脫灰。 – 2010-03-28 23:37:09

回答

1

我認爲解決的辦法可能有問題。每次加載一行都會添加一個事件處理程序,所以如果該行被重用,它可以累積事件處理程序。我建議在行被卸載時刪除事件處理程序。這裏是我的建議代碼:

private void dg_LoadingRow(object sender, DataGridRowEventArgs e) 
{ 
    e.Row.MouseRightButtonDown += new MouseButtonEventHandler(Row_MouseRightButtonDown); 
} 
void Row_MouseRightButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    dg.SelectedItem = ((sender) as DataGridRow).DataContext; 
} 
// new portion 
private void dg_UnloadingRow(object sender, DataGridRowEventArgs e) 
{ 
    e.Row.MouseRightButtonDown -= new MouseButtonEventHandler(Row_MouseRightButtonDown); 
}