右键单击网格行

时间:2010-03-28 09:59:17

标签: silverlight datagrid silverlight-4.0 right-click

问题是,每当右键单击网格的行时,所选项目为null。当右键单击任何行时,我是否选择了网格的行?

感谢 贾马尔

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)