0

我有一个包含很多单元格的网格,其中一些是空的。我想确定鼠标在哪个单元格上,当 MouseDown 事件发生时。这怎么可能?

4

3 回答 3

1

这是另一个似乎比建议的正确答案简单得多的选项。

private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
    var element = (UIElement)e.Source;
    int row = Grid.GetRow(element);
    int column = Grid.GetColumn(element);
}
于 2013-07-24T12:02:06.520 回答
1

首先要记住的是,具有透明背景的控件不会为其透明区域生成事件。为您想要的网格设置颜色,或将其绑定到网格所在窗口的背景颜色,否则事件将不会触发。

此代码示例演示了一种用于确定给定MouseMove事件的网格元素位置的计算方法。事件ButtonClick参数非常相似。此示例中的相关方法是ColumnComputationRowComputation,它们在控制和列或行定义上占据位置,用于线性分析。该示例在InnerGridUI 元素上运行。

表格类:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }



    private void InnerGrid_PreviewMouseMove(object sender, MouseEventArgs e)
    {
        this.XCoordinate.Text = e.GetPosition(InnerGrid).X.ToString();
        this.YCoordinate.Text = e.GetPosition(InnerGrid).Y.ToString();
        this.ColumnPosition.Text = ColumnComputation(InnerGrid.ColumnDefinitions, e.GetPosition(InnerGrid).X).ToString();
        this.RowPosition.Text = RowComputation(InnerGrid.RowDefinitions, e.GetPosition(InnerGrid).Y).ToString();

    }

    private double ColumnComputation(ColumnDefinitionCollection c, double YPosition)
    {
        var columnLeft = 0.0; var columnCount = 0;
        foreach (ColumnDefinition cd in c)
        {
            double actWidth = cd.ActualWidth;
            if (YPosition >= columnLeft && YPosition < (actWidth + columnLeft)) return columnCount;
            columnCount++;
            columnLeft += cd.ActualWidth;
        }
        return (c.Count + 1);
    }
    private double RowComputation(RowDefinitionCollection r, double XPosition)
    {
        var rowTop = 0.0; var rowCount = 0;
        foreach (RowDefinition rd in r)
        {
            double actHeight = rd.ActualHeight;
            if (XPosition >= rowTop && XPosition < (actHeight + rowTop)) return rowCount;
            rowCount++;
            rowTop += rd.ActualHeight;
        }
        return (r.Count + 1);
    }
}

XAML 表单:

<Window x:Name="window" x:Class="GridHitTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="OuterBorder"  >
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Column="0" HorizontalAlignment="Center" >X Coordinate</TextBlock>
        <TextBlock Grid.Column="1" HorizontalAlignment="Center"  >Y Coordinate</TextBlock>
        <TextBlock Grid.Column="2" HorizontalAlignment="Center" >Column</TextBlock>
        <TextBlock Grid.Column="3" HorizontalAlignment="Center"  >Row</TextBlock>
        <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" Name="XCoordinate">kjahsd</TextBlock>
        <TextBlock Grid.Row="1"  Grid.Column="1" HorizontalAlignment="Center" Name="YCoordinate">___ahsdjf</TextBlock>
        <TextBlock Grid.Row="1"  Grid.Column="2" HorizontalAlignment="Center"  Name="ColumnPosition">___ahsdjf</TextBlock>
        <TextBlock Grid.Row="1" Grid.Column="3" HorizontalAlignment="Center"  Name="RowPosition">___ahsdjf</TextBlock>
        <Grid Name="InnerGrid" Margin="20,45,20,10" Grid.ColumnSpan="4" Grid.RowSpan="3" Background="{Binding Background, ElementName=window}"  PreviewMouseMove="InnerGrid_PreviewMouseMove"  >
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
        </Grid>
    </Grid>
</Window>
于 2013-07-23T06:07:31.483 回答
1

将一个按钮放入空单元格(或所有单元格中的按钮,但在其他项目存在时隐藏样式并使单元格不为空)。然后当用户点击单元格时,报告单元格如

private void OnButtonClick(object sender, RoutedEventArgs e)
{
    var buttonClicked = sender as Button;

    var gridRow = (int)buttonClicked.GetValue( MyGrid.RowProperty );
    var gridColumn = (int)buttonClicked.GetValue( MyGrid.ColumnProperty );

}
于 2013-07-22T17:35:44.737 回答