我是新來的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