鼠标向上/点击图像不起作用,除非它有一个来源

本文关键字:有一个 不起作用 图像 鼠标 | 更新日期: 2024-10-22 22:55:42

我有15个图像。。在我有"鼠标向上"的地方,让它打开一个windows资源管理器浏览器窗口,这样他们就可以选择放什么图像作为源。

问题是,如果图像源中最初什么都没有。。鼠标上移不起作用。。。

有办法绕过这个吗?

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);
            }
        }

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);
            }
        }

鼠标向上/点击图像不起作用,除非它有一个来源

default Background for Border is null决定哪个does not respond to HitTest,即不会响应MouseEvents。你所需要的只是为你的边界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>

对此没有解决方法。它是经过设计的。没有源的图像或多或少等于null,因此命中测试(用鼠标点击)不可用。一旦设置了源,图像就可以参与命中测试。

如果您使用一个控件并将其背景色设置为null,您会注意到没有处理单击。

我有一个解决这个问题的办法。通过添加如下所示的边框控件来修改XAML。由于MouseLeftButtonUp是一个附加事件。因此,您可以将此事件挂接到任何FrameworkElement,如下所示。

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

您的代码需要轻微更改,如下

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);
        }
    }

不使用((Image)sender).Source而是将图像直接分配给Image控件。请检查一下这对你来说是否可行。

相关文章: