2012-09-13 82 views
0

我想添加選項以在圖像上繪製矩形。但這不是問題。 爲了讓它工作,我需要使用WPF中Image Control的MouseDown,MouseUp和MouseLeave事件。MouseDown event not fireing

<DockPanel Name="dockPanel1" Grid.ColumnSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="15"> 
     <GroupBox Header="WebCam" Name="groupBox1" VerticalAlignment="Top" HorizontalAlignment="Left"> 
      <Grid> 
       <Image HorizontalAlignment="Left" Margin="0" Name="pictureBoxMain" Stretch="Fill" VerticalAlignment="Top" MinHeight="240" MinWidth="320" StretchDirection="DownOnly" MaxWidth="960" MaxHeight="1200" MouseDown="pictureBoxMain_MouseDown" MouseUp="pictureBoxMain_MouseUp" MouseLeave="pictureBoxMain_MouseLeave" Panel.ZIndex="50" /> 
      </Grid> 
     </GroupBox> 
     <StackPanel HorizontalAlignment="Right" Name="stackPanel2" VerticalAlignment="Top" DockPanel.Dock="Right" Margin="15,0,0,0"> 
      <GroupBox Header="Controls" Name="groupBox2"> 
       <StackPanel Name="stackPanel1"> 
        <Button Content="Start" Height="23" Name="buttonStartStop" Width="120" Margin="0" Click="buttonStartStop_Click" /> 
        <Button Content="Video Properties" Height="23" Name="buttonVideoProperties" Width="120" Margin="0" Click="buttonVideoProperties_Click" /> 
        <Button Content="WebCam Settings" Height="23" Name="buttonWebCamSettings" Width="120" Margin="0" Click="buttonWebCamSettings_Click" /> 
       </StackPanel> 
      </GroupBox> 
      <GroupBox Header="Motion Data" Name="groupBox3"> 
       <StackPanel> 
        <Label Content="Strength: 0" x:Name="labelMotionStrength" /> 
        <Label Content="Count: 0" x:Name="timesWasMotionCounter" /> 
       </StackPanel> 
      </GroupBox> 
     </StackPanel> 
    </DockPanel> 

我在說「pictureBoxMain」的事件。 爲什麼這些事件永遠不會開火?我像瘋了一樣點擊我的圖片,但仍然沒有。 我應該怎麼做才能使這些活動起作用?

+0

嘗試處理PreviewMouseDown來代替(http://stackoverflow.com/questions/2858539/image-mousedown-event-not-firing) – Surfbutler

+0

@Surfbutler仍然無法正常工作。 – Hooch

+1

嘗試將鼠標處理程序放在父網格上,併爲網格指定「透明」的背景屬性。 –

回答

1

我發現的錯誤。 出於某種原因,我的圖片框大小爲0,0。 我伸展它,現在它工作。

1

您是否試過捕捉PreviewMouseDown?

To capture the same as MouseDown you need to use PreviewMouseDown event which is a tunneling event. 
+0

PreviewMouseDown也不會觸發。 – Hooch

0

您需要將圖像的來源屬性設置爲一個圖像文件或事件處理程序之前綁定將工作:

<Image HorizontalAlignment="Left" Margin="0" Name="pictureBoxMain" Stretch="Fill" VerticalAlignment="Top" MinHeight="240" MinWidth="320" StretchDirection="DownOnly" MaxWidth="960" MaxHeight="1200" MouseDown="pictureBoxMain_MouseDown" MouseUp="pictureBoxMain_MouseUp" MouseLeave="pictureBoxMain_MouseLeave" Source="test_image.png" Panel.ZIndex="50" /> 
+0

我使用圖片框來顯示攝像頭視頻幀。圖像稍後在代碼中進行排序。但它仍然沒有工作圖像分配給它。 – Hooch

+0

您可以舉例說明如何在代碼中分配圖像嗎?如果我在buttonStartStop_Click事件處理程序中爲pictureBoxMain.Source賦值而不是在XAML中,則它可以正常工作。 –

+0

BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap,IntPtr.Zero,Int32Rect.Empty,BitmapSizeOptions.FromEmptyOptions()); bmaps.Freeze(); pictureBoxMain.Source = bmaps;這工作正常。只有事件沒有被解僱。 – Hooch