我是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;
}该处理程序是通过blend Events (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被触发时,下面的处理程序应该可以工作,
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代码upthere中以节省空间。
谢谢。
发布于 2012-03-08 00:48:10
我猜原因是您的矩形正在获得UP事件,并且(不知何故?)而不是把它吹到画布上。因此,尝试将ishitestvisible设置为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;
}`
发布于 2012-03-08 19:47:30
它现在可以工作了,尽管我不相信答案,但是我通过双击Blend的events place for MouseUp更改了OnMouseUp函数,它变成了
private void drawingArea_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
_isDrawing = false;
drawingArea.Children.Remove(_selectionBox);
}谁能解释一下是什么导致了这个问题?处理程序的名称有这样的效果吗?我想知道..。
https://stackoverflow.com/questions/9605173
复制相似问题