[英]WPF DataGrid - get row number which mouse cursor is on
我希望在 DataGrid 中獲取鼠標光標所在的行號(所以基本上是在 MouseEnter 事件上),這樣我就可以獲得綁定了 ItemSource 的 DataGridRow 項目,
我為 MouseEvent 擁有的 XAML 是...
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<EventSetter Event="MouseEnter" Handler="Event"></EventSetter>
<Setter Property="ToolTip" Value="{Binding Property}" />
</Style>
</DataGridTextColumn.ElementStyle>
事件本身...
private void Event(object sender, MouseEventArgs e)
{
// I have the DataGrid object itself.
m_DataGrid.?
}
也許我這樣做是不可能的,但如果不能以某種方式完成,我會感到驚訝。
謝謝
如果訪問鼠標懸停的DataGridRow
對象,則可以使用DataGridRow.GetIndex
方法找到其行索引:
private void Event(object sender, MouseEventArgs e)
{
HitTestResult hitTestResult =
VisualTreeHelper.HitTest(m_DataGrid, e.GetPosition(m_DataGrid));
DataGridRow dataGridRow = hitTestResult.VisualHit.GetParentOfType<DataGridRow>();
int index = dataGridRow.GetIndex();
}
GetParentOfType
方法實際上是我使用的擴展方法:
public static T GetParentOfType<T>(this DependencyObject element) where T : DependencyObject
{
Type type = typeof(T);
if (element == null) return null;
DependencyObject parent = VisualTreeHelper.GetParent(element);
if (parent == null && ((FrameworkElement)element).Parent is DependencyObject) parent = ((FrameworkElement)element).Parent;
if (parent == null) return null;
else if (parent.GetType() == type || parent.GetType().IsSubclassOf(type)) return parent as T;
return GetParentOfType<T>(parent);
}
好的,我在這里找到了答案......
http://www.scottlogic.com/blog/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html
不要被文章標題搞糊塗了。。
對於我的解決方案,我基本上使用
private void MouseOverEvent(object sender, MouseEventArgs e)
{
DependencyObject dep = (DependencyObject)e.OriginalSource;
// iteratively traverse the visual tree
while ((dep != null) &&
!(dep is DataGridCell) &&
!(dep is DataGridColumnHeader))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridColumnHeader)
{
DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;
// do something
}
if (dep is DataGridCell)
{
DataGridCell cell = dep as DataGridCell;
// navigate further up the tree
while ((dep != null) && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
DataGridRow row = dep as DataGridRow;
//!!!!!!!!!!!!!* (Look below) !!!!!!!!!!!!!!!!!
}
因此,所有關於使用鼠標原始源並向上看 VisualTree 直到找到正確的元素。
這對我有用。
<DataGrid x:Name="dataGridView">
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseEnter" Handler="Row_MouseEnter"/>
</Style>
</DataGrid.Resources>
</DataGrid>
private void Row_MouseEnter(object sender, MouseEventArgs e)
{
int index = dataGridView.ItemContainerGenerator.IndexFromContainer((DataGridRow)sender);
}
在我的一個應用程序中,我需要鼠標下方的 DataGridRow 來突出顯示它。 每個 Cell 都有一個 DataTemplate
<DataTemplate x:Key="templateCenter">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="10 0 10 0">
<Image Source="{StaticResource Back}" Width="15" Height="15"/>
<Image Source="{StaticResource ListMove}" Width="35" Height="35"/>
<Image Source="{StaticResource Forward}" Width="15" Height="15"/>
</StackPanel>
</DataTemplate>
為了獲取 DataGridCell 的坐標,我在移動鼠標的同時尋找 DataTemplate 的圖像。 如果找到圖像,我可以向上移動 VisualTree 以找到我的單元格,當我獲得圖像時它的坐標(行、列)。 這不是一個非常通用的代碼 - 適合我使用。
private void DragDrop_PreviewDragOver(object sender, DragEventArgs e)
{
if (e.OriginalSource.GetType() != typeof(Image))
{
return;
}
Point destination = GetCellLocation((DataGrid)sender, (StackPanel)((Image)e.OriginalSource).Parent);
}
和這個
private Point GetCellLocation(DataGrid datagrid, StackPanel stackpanel)
{
DataGridCell cell = (DataGridCell)((ContentPresenter)stackpanel.TemplatedParent).Parent;
int column = cell.Column.DisplayIndex;
int row = ((DataGrid)datagrid).Items.IndexOf(stackpanel.DataContext);
return new Point(column, row);
}
希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.