Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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# C/WPF从数据库检索图像并显示它们_C#_Wpf_Database_Binding - Fatal编程技术网

C# C/WPF从数据库检索图像并显示它们

C# C/WPF从数据库检索图像并显示它们,c#,wpf,database,binding,C#,Wpf,Database,Binding,我需要从数据库中检索图像的url,将其绑定到业务对象,将其转换为位图图像,并将其映射到资源字典中。资源字典用作库,我无法更改它 数据库由一个表组成,该表有三列:id、name、url。它是SQL CE 3.5本地数据库。我希望能够一次显示所有图像 我想写的是: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SqlClient; usin

我需要从数据库中检索图像的url,将其绑定到业务对象,将其转换为位图图像,并将其映射到资源字典中。资源字典用作库,我无法更改它

数据库由一个表组成,该表有三列:id、name、url。它是SQL CE 3.5本地数据库。我希望能够一次显示所有图像

我想写的是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Media;



namespace Library.Components
{
    public class ComponentsList : ObservableCollection<Components>
    {
        public ComponentsList()
        {


            String connString = @"Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5; Data Source=local_source";

            String query = @"SELECT id, name, url FROM GraphicComponents";

            // Fill the Set with the data
            using (SqlConnection conn = new SqlConnection(connString))
            {
                try
                {
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = query;

                    conn.Open();
                    SqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {

                        this.Add(new Components(rdr));

                    }
                }
                finally
                {
                    if (conn.State != ConnectionState.Closed) conn.Close();
                }
            }
        }


    }
    public class Components
    {

  int _componentID;
  string _name;
  BitmapImage _url;


  public Components(IDataRecord record)
  {
      _componentID = (int)record["id"];
      _name = (string)record["name"];
      _url = (string)record["url"];

  //Code taken from a tutorial used to convert the url into BitmapImage
    CreatePhoto((byte[])record["url"]);
  }


  void CreatePhoto(byte[] photoSource)
  {

    _url = new System.Windows.Media.Imaging.BitmapImage();
    System.IO.MemoryStream strm = new System.IO.MemoryStream();


    int offset = 78;
    strm.Write(photoSource, offset, photoSource.Length - offset);

    // Read the image into the bitmap object
    _url.BeginInit();
    _url.StreamSource = strm;
    _url.EndInit();

  }
  public int ComponentID
  {
      get { return _componentID; }
  }

  public string Name
  {
      get { return _name; }

  }

  public BitmapImage Photo
  {
      get { return _url; }
  }

    }
}
在XAML的资源字典中,我有图像控件。图像的来源是硬编码的;但我需要从数据库中检索它

提前感谢您的帮助。 代码乱七八糟,我相信有一种更简单的方法可以从数据库中检索图像的URL并将其映射到资源字典中。

为什么不使用


签出:

--第一次尝试,忽略直到“第二次尝试”-- 不要将图像控件放在资源字典中

如果您有固定数量的图像,请直接将它们放在窗口中。 例如:

然后设置一个类,该类的属性Image1和Image2映射到来自数据库的图像,并将该类的初始化实例设置为窗口的DataContext

更现实地说,您拥有数量可变的图像。XAML变为:

<Window>
  <ItemsControl ItemsSource="{Binding Path=Components}">
     <ItemsControl.ItemTemplate>
       <DataTemplate>
          <Image Source="{Binding Path=Photo}" />
       </DataTemplate>
     </ItemsControl.ItemTemplate>
  <ItemsControl>
</Window>
并将DataContext设置为ComponentList的实例

您可以将项模板放入资源字典中。 --第一次尝试结束--

第二次尝试: 在我们的注释交换之后,情况是您在资源字典中定义了一个模板,希望在运行时修改该模板

必须等待模板已应用于承载图像的控件。这似乎是一个工具箱类。我在MSDN文档中找不到它,所以它看起来是定制的,这很好,因为有一个OnApplyTemplate方法可以从中获取此信息。如果无法修改此类,可以调用ApplyTemplate以确保模板已就位

现在,您可以使用VisualTreeHelper.GetChild查看模板中的子控件,以便找到感兴趣的图像。在示例模板中,图像没有name属性。如果你不能添加一个,你需要设计一个策略来识别它们,或者按顺序或者其他一些属性,比如工具提示


一旦找到图像,您可以随意设置其属性。

将其映射到资源字典是什么意思?看来我的评论不对。我有XAML中的资源字典:正如您所看到的,URL是硬编码的。我这里需要一个绑定。我有一个XAML中的资源字典,在另一个地方使用。看起来是这样的:正如您所看到的,URL是硬编码的。我这里需要装订。也许我应该提供更多细节。应用程序用于从资源字典中提供的元素创建图表。您可以拖放它们,然后连接它们,等等。图像被放在资源字典中,我不太可能更改它。如果它们在资源字典中,为什么要从数据库中读取它们?项目要求是我能给出的最接近的答案。它应该被加载到库中。如果我是从头开始写的,它就不会是这样。甚至可以这样使用绑定吗?对不起,我迷路了,你必须使用资源字典,它有硬编码的文件URL,你希望图像来自数据库。这是正确的吗?
<Window>
  <ItemsControl ItemsSource="{Binding Path=Components}">
     <ItemsControl.ItemTemplate>
       <DataTemplate>
          <Image Source="{Binding Path=Photo}" />
       </DataTemplate>
     </ItemsControl.ItemTemplate>
  <ItemsControl>
</Window>