如何以编程方式在 WPF 中的图像上添加 OnClick 事件 [英] How to Add OnClick event on Image in WPF Programmatically
本文介绍了如何以编程方式在 WPF 中的图像上添加 OnClick 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 C# 和 WPF 的新手,我想用 MySQL 作为数据库制作电影库之类的东西,以使用 Stackpanel 显示图像和标题.我不知道如何以编程方式在图像上添加单击事件,因为我没有在 Xaml 中使用该图像.
另外,Stackpanel 可以有 3x3 或 4x4 的网格而不是只有 1 列吗?
I am new to C# and WPF, and I want to make something like Movie Library with MySQL as a database to show the image and the title using Stackpanel.
I don't know how to add click event on an image programmatically Because I'm not using the image in Xaml.
Also, can Stackpanel have 3x3 or 4x4 grid instead only have 1 column?
我的程序截图:
这是我的代码
public void FillData()
{
int id = 1;
for (int i = id; i < 100; i++)
{
MySqlCommand cmd;
cmd = koneksi.CreateCommand();
cmd.CommandText = "select * from movie_list where id_movie = '" + i + "'";
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
if (ds.Tables[0].Rows.Count == 1)
{
string cover = (String)(ds.Tables[0].Rows[0]["cover"]);
string titles = (String)(ds.Tables[0].Rows[0]["title"]);
StackPanel Sp = sp;
StackPanel Sp2 = sp2;
Sp.Orientation = Orientation.Horizontal;
Sp2.Orientation = Orientation.Horizontal;
var picture = new Image
{
Name = "pb" + i,
Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + cover, UriKind.Absolute)),
RenderSize = new Size(100,150),
Margin = new Thickness(20,0,20,0),
};
var title = new Label
{
Name = "sp" +i,
Content = titles,
Width = 120,
Margin = new Thickness(10, 0, 20, 0),
HorizontalContentAlignment = HorizontalAlignment.Center,
};
Sp.Children.Add(picture);
Sp2.Children.Add(title);
}
}
}
推荐答案
var picture = new Image();
picture.MouseLeftButtonUp += (s, e) =>
{
//[your code goes here]
};
这篇关于如何以编程方式在 WPF 中的图像上添加 OnClick 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文