开发者

Need to detect drop location in reference to Grid Element in WPF

开发者 https://www.devze.com 2023-02-06 13:57 出处:网络
I am creating Grid elements dynamically onto a stack panel. When an object is dropped on the stack panel, it fires an event that needs to determine where the drop point was in reference to the Grid el

I am creating Grid elements dynamically onto a stack panel. When an object is dropped on the stack panel, it fires an event that needs to determine where the drop point was in reference to the Grid element on the StackPanel. I am using WPF, so that might make some answers different.

For example, I have the following code that is called when an object is dropped onto a grid element(that has been previously created dynamically).

/*Handles Drop Event From Grid Item*/
    private void Grid_Drop(object sender,DragEventArgs e)
    {
        Object droppedData = e.getData(typeof(Object)); //This part is not important

        /*Get the Grid that was dropped on*/
        Grid grid = (Grid)sender;

        /*Translate Drop Point in reference to Stack Panel*/
        Point dropPoint = this.TranslatePoint(e.getPosition(this),StackPanel));

    }

Here is where I have stopped, I need to calculate 开发者_高级运维something like the following

       IF dropPoint IS ABOVE THE MIDDLE POINT OF THE GRID
            DO SOMETHING
       ELSE IF THE dropPoint IS BELOW THE MIDDLE POINT OF THE GRID
            DO SOMETHING


Why don't you get the relative position to the grid, rather than the stack panel and compare that point.y to grid.actualheight/2 to see if it is above or below?

0

精彩评论

暂无评论...
验证码 换一张
取 消