0
votes

I'm new to Silverlight and I'm working on drawing a rectangle on the screen in the run time,

I have a class ( SelectionBox ), and on the main page, I have a canvas, when clicking on that Canvas I have the following function called

private void BeginDrawing(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _initXPos = e.GetPosition(drawingArea).X;
            _initYPos = e.GetPosition(drawingArea).Y;
            _selectionBox = new SelectionBox ();
            drawingArea.Children.Add(_selectionBox);
            _isDrawing = true;
        }

and that Handler has been added through blend Events (MouseLeftButtonDown)

when the mouse is moving, the following method is called, also added through Blend events,

private void UpdateSelectionBox(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if(!_isDrawing &&)
                return;
            double rectWidth, rectHeight, rectXPos, rectYPos;
            if (e.GetPosition(drawingArea).X >= _initXPos)
            {
                rectWidth = e.GetPosition(drawingArea).X - _initXPos;
                rectXPos = _initXPos;
            }
            else
            {
                rectWidth = _initXPos - e.GetPosition(drawingArea).X;
                rectXPos = e.GetPosition(drawingArea).X;
            }

            if (e.GetPosition(drawingArea).Y >= _initYPos)
            {
                rectHeight = e.GetPosition(drawingArea).Y - _initYPos;
                rectYPos = _initYPos;
            }
            else
            {
                rectHeight = _initYPos - e.GetPosition(drawingArea).Y;
                rectYPos = e.GetPosition(drawingArea).Y;
            }

            _selectionBox.Width = Math.Abs(rectWidth - 20);
            _selectionBox.Height = Math.Abs(rectHeight - 20);
            Canvas.SetLeft(_selectionBox, Math.Abs(rectXPos - 20));
            Canvas.SetTop(_selectionBox, Math.Abs(rectYPos - 20));
        }

when the mouseLeftButtonUp is triggered, the following Handler should work,

private void StopDrawingAndSelect(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _isDrawing = false;
            drawingArea.Children.Remove(_selectionBox);
        }

but unfortunately it never triggers, I put a break point and try to debug it but it never reaches,, I'm not sure Why, this is the XAML for the SelectionBox class

<UserControl
    ...

    <Grid x:Name="LayoutRoot">
        <Rectangle Fill="#00F4F4F5" Stroke="Black" StrokeDashArray="1 2"/>
    </Grid>
</UserControl>

And this is the XAML of the MainPage

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:SelectionBoxTraining"
    x:Class="SelectionBoxTraining.MainPage"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot" Background="White">
        <Canvas Margin="165,0,0,0" x:Name="drawingArea" MouseLeftButtonDown="BeginDrawing" Background="#FF959FD6" MouseMove="UpdateSelectionBox" MouseLeftButtonUp="StopDrawingAndSelect" />

    </Grid>
</UserControl>

Please can anybody help me? I tried a lot of things to do but that didn't work

I hope I find help here,

NOTE: The properties are not included in the XAML code upthere to save space,

Thank You.

2
Oh,, I forgot to say that If I Apply the StopDrawingAndSelect to the RightMouseButtonUp it worksZero

2 Answers

0
votes

I would guess that the reason is that your rectangle is getting the UP event and (somehow?) not bubbling it up to the canvas. So try to set ishitestvisible to false:

private void BeginDrawing(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _initXPos = e.GetPosition(drawingArea).X;
            _initYPos = e.GetPosition(drawingArea).Y;
            _selectionBox = new SelectionBox ();

            _selectionBox.IsHitTestVisible = false;

            drawingArea.Children.Add(_selectionBox);
            _isDrawing = true;
        }

`

0
votes

It's now working, even though I'm not convinced of the answer, but I changed the OnMouseUp Function by double-click on Blend's events place for MouseUp, and it became

private void drawingArea_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _isDrawing = false;
            drawingArea.Children.Remove(_selectionBox);
        }

does anyone has an explanation for what caused the problem? does the name of the handler has that effect? I Wonder..