从X和Y位置获取网格行和列

时间:2012-06-20 09:30:15

标签: wpf grid row

我有一个网格,我知道用鼠标点击的X值和Y值。

示例:X:235,Y:235 - >这应该是Column:3和Row:3

如何获得x和y坐标上的列和行?

我是否真的需要添加行/列的每个宽度,直到达到所需的x / y值?

我正在使用WPF(对于我的网格)& c#(对于x和y位置)

有人可以帮我吗?

2 个答案:

答案 0 :(得分:2)

Math.Floor函数,例如:

Math.Floor(235/scale)
where scale is the the width or height of the grid cell.

定义: 返回小于或等于指定数字的最大整数。 Math.ceil则相反。

答案 1 :(得分:2)

为此,请考虑......

  1. 您的网格有一些背景颜色设置(即使Transparent也可以)。这是用于命中测试(鼠标单击空白网格区域)工作。
  2. GridSplitter本身是网格中的成员。这一点很重要,因为单击网格分割器还会显示网格分割器所在的相应单元格索引(Column,Row)。
  3. 我们不关心此处的ColumnSpanRowSpan
  4. XAML ......

    <Window x:Class="WpfApplication3.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="300" Width="300">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid Mouse.PreviewMouseDown="Grid_MouseDown"
                  Background="Transparent">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
    
                <GridSplitter ResizeDirection="Rows"
                              Width="{Binding ActualWidth,
                                        RelativeSource={RelativeSource
                                              AncestorType=Grid}}"
                              Height="4" Grid.ColumnSpan="3"
                              Background="Red" Grid.Row="1" />                
                <TextBox Height="60" AcceptsReturn="True" 
                         Text="Element1"
                         ScrollViewer.VerticalScrollBarVisibility="Visible" />
                <Button Content="Element2" Grid.Column="1"/>
                <TextBlock Text="Element4" Grid.Row="2" Width="100"
                           Height="40" HorizontalAlignment="Left"
                           VerticalAlignment="Center"/>
                <ComboBox SelectedIndex="0" Height="20"
                          Grid.Column="1" Grid.Row="2">
                    <ComboBoxItem Content="Element5"/>
                </ComboBox>
                <CheckBox Content="Element3" Grid.Column="2"/>
                <RadioButton Content="Element6" Grid.Row="2"
                             Grid.Column="2" VerticalAlignment="Center"
                             HorizontalAlignment="Center"/>
            </Grid>
            <StackPanel Orientation="Horizontal" Grid.Row="1">
                <TextBlock Text="Selected Column and Row is ... " Margin="5"/>
                <TextBlock x:Name="StatusTextBlock" FontSize="12"
                           FontWeight="SemiBold" Margin="5"/>
            </StackPanel>
        </Grid>
    </Window>
    

    代码背后......

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            int selectedColumnIndex = -1, selectedRowIndex = -1;
            var grid = sender as Grid;
            if (grid != null)
            {
                var pos = e.GetPosition(grid);
                var temp = pos.X;
                for (var i = 0; i < grid.ColumnDefinitions.Count; i++ )
                {
                    var colDef = grid.ColumnDefinitions[i];
                    temp -= colDef.ActualWidth;
                    if (temp <= -1)
                    {
                        selectedColumnIndex = i;
                        break;
                    }
                }
    
                temp = pos.Y;
                for (var i = 0; i < grid.RowDefinitions.Count; i++)
                {
                    var rowDef = grid.RowDefinitions[i];
                    temp -= rowDef.ActualHeight;
                    if (temp <= -1)
                    {
                        selectedRowIndex = i;
                        break;
                    }
                }
            }
    
            StatusTextBlock.Text = selectedColumnIndex + ", " + selectedRowIndex;
        }