WPF 将值发送到动态图像后面的代码?

我是使用 WPF 表单的新手,我创建了一个页面来显示数据库中的图像,但是尽管搜索了几天,我还是找不到一种方法来知道哪个图像上有鼠标悬停或鼠标单击事件。


要设置我拥有的图像:


public class RBimageData

    {

        private string _Title;

        public string Title

        {

            get { return this._Title; }

            set { this._Title = value; }

        }


        private BitmapImage _ImageData;

        public BitmapImage ImageData

        {

            get { return this._ImageData; }

            set { this._ImageData = value; }

        }


        private String _ImageID;

        public String ImageID

        {

            get { return this._ImageID; }

            set { this._ImageID = value; }

        }

    }


    public MainWindow()

    {

        InitializeComponent();

        RBpartsList rbPartsList = mongoDB.GetRBparts("elements", 1, 7); // get parts from database


        List<RBpartsImages> rbImages = rbPartsList.RBparts;


        List<RBimageData> md = new List<RBimageData>();           

        foreach (RBpartsImages img in rbImages)

        {

            RBimageData m = new RBimageData

            {

                Title = img.ImageFilename,

                ImageID = "id_"+img.PartNum,

                ImageData = LoadImage(rbPartsList.FilePath,img.ImageFilename) }; // provides BitmapImage URI for image                

                md.Add(m);

        }


        RBbox.ItemsSource = md.ToArray();

它不适用于 x:Name="{Binding ImageID}".. 我找不到任何可以让我识别点击了哪个图像的东西,你能帮忙吗?



精慕HU
浏览 162回答 1
1回答

互换的青春

当您this在代码隐藏中引用时,它指向 Window 对象。您正在寻找控件的Tag属性Image。为此,我建议在控件上定义MouseEnter="IdentifyPartImage_MouseEnter"和MouseLeave="IdentifyPartImage_MouseLeave"事件Image,然后sender参数将是该Image对象。在您的 XAML 中:<Button>&nbsp; &nbsp; <Image Source="{Binding ImageData}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HorizontalAlignment="Stretch"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VerticalAlignment="Top"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Stretch="UniformToFill"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Tag="{Binding ImageID}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MouseEnter="IdentifyPartImage_MouseEnter"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MouseLeave="IdentifyPartImage_MouseLeave"/>&nbsp;</Button>在你的代码隐藏中:private void IdentifyPartImage_MouseEnter(object sender, MouseEventArgs e){&nbsp; &nbsp; var imgId = ((Image)sender).Tag.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; Debug.WriteLine("id: {0}, {1}", "imageID", imgId);}
打开App,查看更多内容
随时随地看视频慕课网APP