Skip to main content
Home Forums Silverlight Programming Report a Silverlight Bug Bug in Rectangle Drawing (SL2 B1)
2 replies. Latest Post by NilK29 on May 16, 2008.
(0)
NilK29
Member
22 points
8 Posts
05-14-2008 10:46 AM |
All,
Please check out the following simple code. If you hold down mouse button and drag mouse thru canvas, the rectangle is expected to draw/refresh itself. It does draw rectangle "except the Stroke" used for border. If you notice there is a small "Aaua" dot at left/top corner when you start dragging indicating the presence of stroke. Can somebody provide a solution to this issue?
Thanks
Nilk
<UserControl x:Class="Tryouts.Page" xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <Canvas Width="400" Height="300" Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="1" Background="DarkBlue" MouseLeftButtonDown="MyMouseLeftButtonDown" MouseLeftButtonUp="MyMouseLeftButtonUp" MouseMove="MyMouseMove">
<Button x:Name="BtnTest" Click="BtnTest_Click" Width="200" Height="30" Canvas.Left="150" Canvas.Top="5" Content="Move Mouse in Blue Area"></Button> <Rectangle x:Name="RecSelection" Height="100" Width="100" Canvas.Left="0" Canvas.Top="0" Canvas.ZIndex="10" Fill="#7F92B5F4" Stroke="Aqua" StrokeThickness="1" Visibility="Collapsed"/> </Canvas></UserControl>
using System;using System.Collections.Generic;using System.Linq;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;
namespace Tryouts{ public partial class Page : UserControl { Point MouseBeginPosition;
public Page() { InitializeComponent(); }
private void BtnTest_Click(object sender, RoutedEventArgs e) { this.RecSelection.Width = 30; this.RecSelection.Height = 200; this.RecSelection.Visibility = Visibility.Visible; }
private void MyMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { MouseBeginPosition = e.GetPosition(this); if (RecSelection.Visibility == Visibility.Collapsed) { ResetRectangle(MouseBeginPosition.Y, MouseBeginPosition.X); RecSelection.Visibility = Visibility.Visible; } }
private void MyMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (RecSelection.Visibility == Visibility.Visible) { RecSelection.Visibility = Visibility.Collapsed; ResetRectangle(-1, -1); }
MouseBeginPosition = e.GetPosition(this);
}
private void MyMouseMove(object sender, MouseEventArgs e) { Point CurrentLocation = e.GetPosition(this);
if (RecSelection.Visibility == Visibility.Visible) DrawSelectionRectangle(MouseBeginPosition, CurrentLocation);
private void DrawSelectionRectangle(Point From, Point To) { RecSelection.Visibility = Visibility.Visible; RecSelection.Width = Math.Abs(From.X - To.X); RecSelection.Height = Math.Abs(From.Y - To.Y); if (From.X > To.X) RecSelection.SetValue(Canvas.LeftProperty, To.X); else RecSelection.SetValue(Canvas.LeftProperty, From.X);
if (From.Y > To.Y) RecSelection.SetValue(Canvas.TopProperty, To.Y); else RecSelection.SetValue(Canvas.TopProperty, From.Y);
RecSelection.Visibility = Visibility.Visible; }
private void ResetRectangle(double Top, double Left) { if (Top > -1) { RecSelection.SetValue(Canvas.TopProperty, Top); } if (Left > -1) { RecSelection.SetValue(Canvas.LeftProperty, Left); } RecSelection.Width = 0; RecSelection.Height = 0; RecSelection.Visibility = Visibility.Visible; } }}
Yi-Lun L...
All-Star
25052 points
2,747 Posts
05-16-2008 3:42 AM |
Hello, thanks for reporting this issue. It's a known problem which our developers are investigating. This only happens when the Rectangle's Width and Height are both 0. A simple workaround is to set either Width or Height to a little larger than 0, such as 0.01, in the ResetRectangle method.
05-16-2008 9:00 AM |
Yi-Lun Luo,
Thanks for the update. The workaround is prefect. I worked like champ.
Regards