MouseDown事件未触发

时间:2012-09-13 20:00:20

标签: c# .net wpf events

我想添加选项以在图像上绘制矩形。但这不是问题。 为了使它工作,我需要在WPF中使用Image Control的MouseDown,MouseUp和MouseLeave事件。

    <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”的事件。 为什么这些事件从未开火我点击我的图像就像疯了一样,但仍然没有。 我应该怎么做才能使这些活动有效?

3 个答案:

答案 0 :(得分:1)

您是否尝试过捕捉PreviewMouseDown?

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

答案 1 :(得分:1)

我发现了错误。 出于某种原因,我的图片框大小为0,0。 我伸展它现在它起作用了。

答案 2 :(得分:0)

在事件处理程序工作之前,您需要将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" />