我是新来的Silverlight和我工作在运行时在屏幕上绘制一个矩形,的Silverlight:的MouseLeftButtonUp不会被触发
我有一个类(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)
当鼠标移动时,以下的方法是添加,当点击所谓,也加编辑通过混合事件,
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被触发时,下面的处理程序应该工作,
private void StopDrawingAndSelect(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
_isDrawing = false;
drawingArea.Children.Remove(_selectionBox);
}
但不幸的是它永远不会触发,我把一个破发点,并尝试调试它,但它从来没有到达,,我不知道为什么,这是对SelectionBox类
<UserControl
...
<Grid x:Name="LayoutRoot">
<Rectangle Fill="#00F4F4F5" Stroke="Black" StrokeDashArray="1 2"/>
</Grid>
</UserControl>
的XAML这是
的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代码upthere以节省空间,
谢谢您。
哦,我忘了说,如果我将StopDrawingAndSelect应用到RightMouseButtonUp它的工作 – Zero 2012-03-07 16:20:14