mouseleave事件在wpf中被多次调用c#

时间:2015-02-18 09:07:17

标签: c# wpf

我在Tab中有图像,两个事件都附有mouseenter和mouseleave,一旦鼠标离开图像鼠标离开事件应该被调用一次,但它被调用3至4次如何避免这种情况。

下面是我的代码

private void AddImageInTab(TabItemEx tiex, ref int Col, ref int Row, int j, System.Windows.Controls.Image bmp, XmlNode Node, WrapPanel wrapPanel)
        {
            try
            {
                //some logic here
                bmp.MouseLeave += bmp_MouseLeave;
                bmp.MouseEnter += bmp_MouseEnter;
            }
            catch (Exception ex)
            {
                Validate.LogException(ex);
            }
        }

    private int mouseEnterCount = 0;

    void bmp_MouseLeave(object sender, MouseEventArgs e)
    {
        if (mouseEnterCount == 1)
        {
            mouseEnterCount = 0;
            if (tooltip != null)
            {
                tooltip.Close();
                tooltip = null;
                e.Handled = true;
            } 
        }
    }

    void bmp_MouseEnter(object sender, MouseEventArgs e)
    {
        if (++mouseEnterCount == 1)
        {
            if (tooltip != null)
                e.Handled = true;

            if (tooltip == null || !tooltip.IsOpen)
            {
                Image img = ((Image)sender);
                if (img.Name != string.Empty)
                {
                    string[] values = ((Image)sender).Tag.ToString().Split(new[] { SPLIT_PATTERN }, StringSplitOptions.None);
                    string sizesTemp = GetSizesInFormat(values[4]);

                    if (values[0] != "pipeTypeItems" && values[values.Length - 2] == "False")
                    {
                        img.Cursor = Cursors.No;
                        tooltip_Information(img, sizesTemp);
                    }
                    else
                        tooltip_Information(img, sizesTemp);
                }
            }
            else
            {
                tooltip.ResetTimer();
            } 
        }
    }

1 个答案:

答案 0 :(得分:0)

您正在尝试将WPF与WinForms结合使用。 WPF并不意味着以这种方式使用。

首先设计您的视图模型:

public class ImageViewModel {
    public string Name { get; set; }
    public BitmapImage Bitmap{ get; set; }
    public string BitmapInfo { 
        get { 
            return string.Format("{0}x{1}", Bitmap.PixelWidth, Bitmap.PixelHeight);
        }
    }
    public bool CanBrowse { get; set; }
    // ...
}

然后设计你的观点:

<Window ...>
  <TabControl ItemsSource="{Binding}">
    <TabControl.ItemTemplate>
      <DataTemplate>
        <TextBlock Text="{Binding Name}"/>
      </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
      <DataTemplate>
        <Image 
          Source="{Binding Bitmap}"
          ToolTip="{Binding BitmapInfo}"
          Cursor="{Binding CanBrowse, Converter={x:Static BoolToCursorConverter.Instance}}"
          />
      </DataTemplate>
    </TabControl.ContentTemplate>
  </TabControl>
</Window>

将它们绑在一起

var images = new ObservableCollection<ImageViewModel>();

// fill images

var view = new ImagesView();
view.DataContext = images;