简体   繁体   中英

MouseDown event not fireing

I want to add option to draw rectangle on image. But that is not problem. In order to get it working I need to use MouseDown, MouseUp and MouseLeave events of Image Control in WPF.

    <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>

I'm talking about events of "pictureBoxMain". Why those events never fire? I'm clicking on my image like crazy and still nothing. What should I do to make those event working?

你试过捕捉PreviewMouseDown吗?

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

I found the error. For some reason my picture box size was 0,0. I stretched it and now it works.

您需要在事件处理程序工作之前将Image的Source属性设置为图像文件或绑定:

<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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM