【发布时间】:2018-11-21 23:08:00
【问题描述】:
我是 C# 和 WPF 的新手,我想用 MySQL 作为数据库制作电影库之类的东西,以使用 Stackpanel 显示图像和标题。
我不知道如何以编程方式在图像上添加点击事件,因为我没有在 Xaml 中使用图像。
另外,Stackpanel 是否可以有 3x3 或 4x4 网格而不是只有 1 列?
我的程序截图:
这是我的代码
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);
}
}
}
【问题讨论】:
-
当您想使用
3x3或4x4网格时 - 为什么不使用Grid而不是StackPanel呢? - docs.microsoft.com/en-us/dotnet/api/… -
只是要爱上这个事实 - 3 个答案和 3 个不同的事件:D
-
@RandRandom 确实如此!让我们看看哪个赛事获胜...
-
问题是,如果我使用网格,我必须在 xaml 中添加图像,我不知道如何自动显示多个图像
-
@Hans - 抱歉,如果我理解正确,这是不正确的 - 仅仅因为您使用的是
Grid而不是StackPanel不会强制您在 XAML 中添加图像 -您也可以通过编程方式创建一个Grid,而 XAML 中不需要任何内容 - 也许可以通过使用Grid来扩展或针对您面临的问题提出不同的问题StackPanel是错误的选择