通过鼠标点击获取网格单元格 [英] Get Grid Cell by mouse click
问题描述
我有一个WPF网格被分成3行3列,$ B $双向无法找到获得的净,喔鼠标点击的行和列数的方式,如果有可能这将是我的程序更好,这部分将在代码,而不是XAML,
,这是我简单的网格:
<电网NAME =GridCtrlShowGridLines =真>
< Grid.RowDefinitions>
< RowDefinition HEIGHT =3 */>
< RowDefinition HEIGHT =3 */>
< RowDefinition HEIGHT =3 */>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =3 */>
< ColumnDefinition WIDTH =3 */>
< ColumnDefinition WIDTH =3 */>
< /Grid.ColumnDefinitions>
< /网格和GT;
与我想出了这个解决方案同样面临的问题
XAML:
<电网NAME =myGrid背景=透明的PreviewMouseLeftButtonDown =OnPreviewMouseLeftButtonDown>
注:电网
必须给予背景,以提高鼠标按下事件,请参阅:的如何获得一个网格,以提高MouseDown事件中,当细胞无uIElemets点击?
代码 - 背后:
私人无效OnPreviewMouseLeftButtonDown(对象发件人,System.Windows.Input.MouseButtonEventArgs E)
{
如果(e.ClickCount == 2)//进行双击,如果只是想删除此条件下单一的点击
{
VAR点= Mouse.GetPosition(myGrid);
INT行= 0;
INT山坳= 0;
双accumulatedHeight = 0.0;
双accumulatedWidth = 0.0;
//计算行鼠标滑过
的foreach(在myGrid.RowDefinitions VAR rowDefinition)
{
accumulatedHeight + = rowDefinition.ActualHeight;
如果(accumulatedHeight> = point.Y)
中断;
行++;
}
//钙山坳鼠标滑过
的foreach(在myGrid.ColumnDefinitions VAR columnDefinition)
{
accumulatedWidth + = columnDefinition.ActualWidth;
如果(accumulatedWidth> = point.X)
中断;
山坳++;
}
// ROW和COL现在对应Grid的RowDefinition和ColumnDefinition鼠标是
//当双击了过来!
}
}
I have a WPF Grid which is divided into 3 rows and 3 columns, i wasn't able to find a way of getting the row and column number of mouse click on the net, ohh and if it is possible it will be better for my program that this part will be in code and not XAML, this is my simple grid:
<Grid Name="GridCtrl" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="3*" />
<RowDefinition Height="3*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
</Grid>
Faced with the same problem I came up with this solution:
XAML:
<Grid Name="myGrid" Background="Transparent" PreviewMouseLeftButtonDown="OnPreviewMouseLeftButtonDown">
NOTE: The Grid
has to be given a background to raise the mouse down event, see: How to get a Grid to raise MouseDown events when no UIElemets in cells clicked?
Code-behind:
private void OnPreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if(e.ClickCount == 2) // for double-click, remove this condition if only want single click
{
var point = Mouse.GetPosition(myGrid);
int row = 0;
int col = 0;
double accumulatedHeight = 0.0;
double accumulatedWidth = 0.0;
// calc row mouse was over
foreach (var rowDefinition in myGrid.RowDefinitions)
{
accumulatedHeight += rowDefinition.ActualHeight;
if (accumulatedHeight >= point.Y)
break;
row++;
}
// calc col mouse was over
foreach (var columnDefinition in myGrid.ColumnDefinitions)
{
accumulatedWidth += columnDefinition.ActualWidth;
if (accumulatedWidth >= point.X)
break;
col++;
}
// row and col now correspond Grid's RowDefinition and ColumnDefinition mouse was
// over when double clicked!
}
}
这篇关于通过鼠标点击获取网格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!