简体   繁体   English

除非有源,否则MouseUp / Click on Image不起作用

[英]MouseUp / Click on Image does not work unless it has a Source

I have 15 images.. where I have "On Mouse Up", have it open up a windows explorer browser window so they can choose what image to put as the source. 我有15张图片..我有“On Mouse Up”,让它打开一个Windows资源管理器浏览器窗口,这样他们就可以选择放置哪个图像作为源。

the thing is if there is nothing in the image source originally.. mouse up doesn't work... 问题是如果原始图像源中没有任何内容..鼠标按钮不起作用...

is there any way around this? 有没有办法解决?

private void Image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            // Configure open file dialog box 
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name 
            dlg.DefaultExt = ".png"; // Default file extension 
            dlg.Filter = "PNG Files (.png)|*.png|TGA Files (.tga)|*.tga|All Files (*.*)|*.*"; // Filter files by extension 

            // Show open file dialog box 
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results 
            if (result == true)
            {
                ((Image)sender).Source = (ImageSource)new ImageSourceConverter().ConvertFromString(dlg.FileName);
            }
        }

Edit for Vimal CK: 编辑Vimal CK:

<GroupBox Width="75" Height="75">
                            <Border MouseLeftButtonUp="Image_MouseUp1">
                                <Image Name="RedPick5_Image" Height="45" Width="45"></Image>
                            </Border>
                        </GroupBox>

private void Image_MouseUp1(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("in here");
            // Configure open file dialog box 
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name 
            dlg.DefaultExt = ".png"; // Default file extension 
            dlg.Filter = "PNG Files (.png)|*.png|TGA Files (.tga)|*.tga|All Files (*.*)|*.*"; // Filter files by extension 

            // Show open file dialog box 
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results 
            if (result == true)
            {
                RedPick5_Image.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(dlg.FileName);
            }
        }

There is no workaround to this. 没有解决方法。 It is by design. 这是设计的。 An Image without Source is more or less equal to null and so hit testing (clicking with mouse around) is not available. 没有源的图像或多或少等于null,因此无法使用命中测试(用鼠标点击)。 As soon you set the Source the Image will have the capability to participate in hit testing. 一旦设置了源,图像就能够参与命中测试。

If you would take a control and set its background color to null you would notice that clicking is not being handled. 如果您将控件并将其背景颜色设置为null,您会注意到没有处理单击。

I have a work around on this issue. 我有一个解决这个问题的方法。 Modify your XAML by adding a border control like below. 通过添加如下边框控件来修改您的XAML。 Since the MouseLeftButtonUp is an attached event. 由于MouseLeftButtonUp是附加事件。 So you can hook this event to any FrameworkElement like below. 因此,您可以将此事件挂钩到任何FrameworkElement如下所示。

<Border MouseLeftButtonUp="Image1_MouseUp">
   <Image Name="Image1"
          Width="200"
          Height="200">
   </Image>
</Border>

Your code required a slight change as follows 您的代码需要稍作更改,如下所示

private void Image1_MouseUp(object sender, MouseButtonEventArgs e)
{
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        dlg.FileName = ""; // Default file name 
        dlg.DefaultExt = ".png"; // Default file extension 
        dlg.Filter = "PNG Files (.png)|*.png|TGA Files (.tga)|*.tga|All Files (*.*)|*.*"; // Filter files by extension 

        // Show open file dialog box 
        Nullable<bool> result = dlg.ShowDialog();

        // Process open file dialog box results 
        if (result == true)
        {
            Image1.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(dlg.FileName);
        }

    }

instead of using ((Image)sender).Source , assign the image directly to Image control. 而不是使用((Image)sender).Source ,将图像直接分配给Image控件。 Please check if it feasible for you. 请检查一下是否可行。

By default Background for Border is null which does not respond to HitTest ie won't respond to MouseEvents. default Background for Border is nulldoes not respond to HitTest即不响应MouseEvents。 All you need is to set Background to Transparent for your border which will enable it for hit test scenarios. 您只需要为边框set Background to Transparent ,这将使其适用于命中测试场景。

<GroupBox Width="75" Height="75">
   <Border MouseLeftButtonUp="Image_MouseUp1" Background="Transparent">
       <Image Name="RedPick5_Image" Height="45" Width="45"></Image>
   </Border>
</GroupBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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