Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mouseleave事件在wpf c中被多次调用#_C#_Wpf - Fatal编程技术网

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

C# mouseleave事件在wpf c中被多次调用#,c#,wpf,C#,Wpf,我在选项卡中有图像,两个事件随附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) {

我在选项卡中有图像,两个事件随附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();
            } 
        }
    }

您试图在考虑WinForms的情况下使用WPF。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;
var images=新的ObservableCollection();
//填充图像
var view=newimagesview();
view.DataContext=图像;

如何以及何时附加事件处理程序?首先显示您的代码。您是否已多次附加到事件?请给我们一些代码好吗?您如何以及在何处将bmp_MouseLeave附加到事件?我已编辑文章,在函数AddImageInTab中我正在附加MouseLeave事件。