【问题标题】:How to Add OnClick event on Image in WPF Programmatically [duplicate]如何以编程方式在 WPF 中的图像上添加 OnClick 事件 [重复]
【发布时间】: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);
        }
    }                
} 

【问题讨论】:

  • 当您想使用 3x34x4 网格时 - 为什么不使用 Grid 而不是 StackPanel 呢? - docs.microsoft.com/en-us/dotnet/api/…
  • 只是要爱上这个事实 - 3 个答案和 3 个不同的事件:D
  • @RandRandom 确实如此!让我们看看哪个赛事获胜...
  • 问题是,如果我使用网格,我必须在 xaml 中添加图像,我不知道如何自动显示多个图像
  • @Hans - 抱歉,如果我理解正确,这是不正确的 - 仅仅因为您使用的是 Grid 而不是 StackPanel 不会强制您在 XAML 中添加图像 -您也可以通过编程方式创建一个Grid,而 XAML 中不需要任何内容​​ - 也许可以通过使用 Grid 来扩展或针对您面临的问题提出不同的问题 StackPanel 是错误的选择

标签: c# wpf xaml


【解决方案1】:

您确实应该将数据访问代码与您的 UI 代码分开。

在 UI 中,使用 ItemsControl 并将 ItemsSource 设置为您的 DataSet。使用每个项目所需的控件将 ItemsTemplate 设置为 DateTemplate。

使用 ListBox 将处理项目选择,而不用担心 Image 和其他控件的点击事件。

如果您想要网格布局而不是直线列表,您可以使用 UniformGrid 作为 ItemsPanelTemplate。

【讨论】:

    【解决方案2】:

    您可以将事件处理程序添加到您正在创建的picture 对象:

    picture.MouseUp += (s, e) => {
        // Do something funny.
    };
    

    【讨论】:

      【解决方案3】:
              var picture = new Image();
              picture.MouseLeftButtonUp += (s, e) =>
              {
                  //[your code goes here]
              };
      

      【讨论】:

      • 此外,如果您真的想表现得像点击一样,您可以将鼠标向上和向下事件与计时器相结合.... ;)
      【解决方案4】:

      除了您绝对不应该将您的数据库代码与您的 UI 代码混合使用之外,您只需订阅 Image 上的 MouseDown(或 MouseUp,如果它应该像点击)事件,然后执行任何你想要的:

      picture.MouseDown += (sender, args) => 
      { 
        Foo();
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-07
        • 2014-03-20
        • 2018-05-26
        • 1970-01-01
        • 2016-10-09
        • 1970-01-01
        相关资源
        最近更新 更多