【发布时间】:2012-03-07 16:17:08
【问题描述】:
我是 Silverlight 的新手,正在运行时在屏幕上绘制一个矩形,
我有一个类( SelectionBox ),在主页上,我有一个画布, 单击该画布时,我调用了以下函数
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;
}
并且该处理程序已通过混合事件添加(MouseLeftButtonDown)
当鼠标移动时,调用下面的方法,也是通过Blend事件添加的,
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));
}
当 mouseLeftButtonUp 被触发时,下面的 Handler 应该工作了,
private void StopDrawingAndSelect(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
_isDrawing = false;
drawingArea.Children.Remove(_selectionBox);
}
但不幸的是它永远不会触发,我设置了一个断点并尝试调试它,但它永远不会到达,我不确定为什么,这是 SelectionBox 类的 XAML
<UserControl
...
<Grid x:Name="LayoutRoot">
<Rectangle Fill="#00F4F4F5" Stroke="Black" StrokeDashArray="1 2"/>
</Grid>
</UserControl>
这是 MainPage 的 XAML
<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>
请问有人可以帮助我吗?我尝试了很多事情要做,但没有奏效
希望能在这里得到帮助,
注意:为了节省空间,XAML 代码中不包含这些属性,
谢谢。
【问题讨论】:
-
哦,我忘了说如果我将 StopDrawingAndSelect 应用到 RightMouseButtonUp 就可以了
标签: silverlight drawing mouseleftbuttonup