0

I have a Grid. I want to calculate the distance between any 2 cells.

Ex:

cell1: (0,1), cell2: (2, 2) ; now the distance is row_distance = 2-0 = 2, col_distance = 2-1 = 1;

I want to find row_distance and col_distance. I have thought a solution as given below:

mouse left button down at first cell(PreviewMouseLeftButtonDown), then mouse left button up at second cell(PreviewMouseLeftButtonUn).

<Grid ShowGridLines="True"   PreviewMouseLeftButtonDown="grid1_PreviewMouseLeftButtonDown"  PreviewMouseLeftButtonUp="grid1_PreviewMouseLeftButtonUp">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="52*" />
                <ColumnDefinition Width="50*" />
                <ColumnDefinition Width="48*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="33*" />
                <RowDefinition Height="39*" />
                <RowDefinition Height="51*" />
            </Grid.RowDefinitions>
        </Grid>

But I could not able to implement function grid1_PreviewMouseLeftButtonDown grid1_PreviewMouseLeftButtonUp. I shall be obliged if anyone can help to implement those functions

Please let me know if there is other solution

2 Answers 2

0

Quoting from MSDN:

This is an attached event. WPF implements attached events as routed events. Attached events are fundamentally a XAML language concept for referencing events that can be handled on objects that do not define that event, which WPF expands upon by also enabling the event to traverse a route. Attached events do not have a direct handling syntax in code; to attach handlers for a routed event in code, you use a designated Add*Handler method. For details, see Attached Events Overview.

0

for the row distance

int getRowDistance(cell cell1, cell cell2){ //IDK if the cells are objects/structs
    int i,offsetWidth;
    for (i = 0;i < cell1.rowNumber; i++){ //This loop will add the width from 0 to the beginning of the first cell

        cell offCell = cellAtRow(i);
        cellWidth += offCell.width;

    }

    int width = 0;
    for (i = cell1.rowNumber;i < cell2.rowNumber; i++){ //This loop will add the width between the two cells

        cell tempCell = cellAtRow(i);
        width += tempCell.width;

    }
    return width - offsetWidth;
}

I hope this makes sense. From what I get off your question, this is what you're looking for. Sorry that this is in plain C, it's the easiest for me to do off the top of my head.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.