当鼠标离开一行时,我有一个鼠标离开事件
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseLeave" Handler="Row_MouseLeave"></EventSetter>
</Style>
</DataGrid.RowStyle>因此,在处理程序中,我尝试获取绑定到行的下划线项
private void Row_MouseLeave(object sender, MouseEventArgs args)
{
DataGridRow dgr = sender as DataGridRow;
<T> = dgr.Item as <T>;
}但是,该项是占位符对象,而不是项本身。
通常,您可以通过DataGrid selectedIndex属性执行我想要的操作。
DataGridRow dgr = (DataGridRow)(dg.ItemContainerGenerator.ContainerFromIndex(dg.SelectedIndex));
<T> = dgr.Item as <T>但是由于ItemSource绑定到DataGrid,而不是DataGridRow,所以DataGridRow不能看到绑定到网格的集合……(我假设)
但是因为我没有选择一行,所以我真的不能这样做。所以有没有办法让我做我想做的事?
干杯
发布于 2014-08-21 16:51:52
如果将事件处理程序附加到DataGridRow.MouseLeave事件,则sender输入参数将是您正确显示的DataGridRow。然而,在那之后,你就错了。DataGridRow.Item属性将从DataGridRow内部返回数据项,除非您将鼠标悬停在DataGrid中的最后一行(空行或新行)上...在这种情况下,DataGridRow.Item属性将返回MS.Internal.NamedObject类型的{NewItemPlaceholder}
private void Row_MouseLeave(object sender, MouseEventArgs args)
{
DataGridRow dataGridRow = sender as DataGridRow;
if (dataGridRow.Item is YourClass)
{
YourClass yourItem = dataGridRow.Item as YourClass;
}
else if (dataGridRow.Item is MS.Internal.NamedObject)
{
// Item is new placeholder
}
}尝试将鼠标悬停在实际包含数据的行上,然后应该会在DataGridRow.Item属性中找到该数据对象。
https://stackoverflow.com/questions/25421602
复制相似问题