2

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

示例:X:235,Y:235 --> 这应该是列:3 和行:3

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

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

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

有人可以帮我吗?

4

2 回答 2

3

Math.Floor 函数,例如:

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

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

于 2012-06-20T09:50:24.007 回答
2

为此,请考虑...

  1. 您的网格设置了一些背景颜色(甚至Transparent可以)。这是为了命中测试(鼠标点击空白网格区域)工作。
  2. GridSplitter本身就是网格中的一个成员。了解这一点很重要,因为单击网格拆分器还会显示网格拆分器所在的相应单元格索引(列、行)。
  3. 我们不关心ColumnSpanRowSpan这里。

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;
    }
于 2012-06-20T12:22:06.613 回答