繁体   English   中英

如何在C#中将图像列表分配给image属性

[英]how to assign list of images to image attribute in c#

我有图像列表,并希望将它们分配给图像属性以显示在数据表中。 我怎样才能做到这一点?
我的想法是我想从数据库中检索值,并依赖于我要显示图像的值。 例如; 如果值= 2,则显示2张图像; 如果= 3,则显示3张图像...

我得到了这一点:无法将System.Collections.Generic.List类型隐式转换为System.Windows.Controls.Image的语句:Project.team_members = HandCards;

这是我的代码:

项目类别

public class Projects
    {
        public string developer_name { get; set; }
        public  Image team_members { get; set; }
    }

ASMX

List<string> developerNames = new List<string>();
List<Image> HandCards = new List<Image>();

Image image = new Image();
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri("images/img.jpg", UriKind.Relative);
source.EndInit();
image.Source = source;

while (somthing)
{
  developerNames.Add((string)rdrDev["user_name"]);//value returned from DB
  HandCards.Add(image);
}

Project.developer_name = string.Join(", ", developerNames);//I want to do 
//something like this for the images. this statement put all developers 
//together seperated by ','; I want to do the same to the next statement but 
//don't know how to do it for image type
Project.team_members = HandCards;

ASPX

var datatableVariable = $('#projects-progress').DataTable({
  data: data,
  columns: [
         { 'data': 'developer_name' },
         { 'data': 'team_member' },
]

我已经有一段时间没有使用过ASP了,但是对于您的问题的第一部分,您可以遵循与以下所示类似的实现:

在HTML标记/ ASPX页面中:说您有三个图像,即。

<img src="~/Img1.jpg" runat="server" />
<img src="~/Img2.jpg" runat="server" />
<img src="~/Img3.jpg" runat="server" />

在后面的C#代码中:

string filePath1 = "Img1.jpg"; //You'll do the same for the other two images
Image img1 = new Image(string filePath1);
Image img2 = new Image(string filePath2);
Image img3 = new Image(string filePath3);

List<Image> listOfImageCollection = new List<Image>();
listOfImageCollection.Add(img1);
listOfImageCollection.Add(img2);
listOfImageCollection.Add(img3);

DataTable dt = new DataTable();

foreach(Image imgs in listOfImageCollection)
{
    dt.DataSource = imgs;
    dt.DataBind();
}

string myConnectionString = "";
SqlConnection mySQLConnection;
SqlCommand mySQLCommand;
SqlDataReader mySQLDataReader;



myConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringToTheDB"].ConnectionString;
                using (mySQLConnection = new SqlConnection(myConnectionString))
                {
                    mySQLCommand = new SqlCommand("storedProcedureNameToGetValue", mySQLConnection);
                    mySQLCommand.CommandType = CommandType.StoredProcedure;
                    mySqlParameter = new SqlParameter();
                    mySqlParameter.ParameterName = "@Value";
                    mySqlParameter.Value = Value;
                    mySQLCommand.Parameters.Add(mySqlParameter);
                    mySQLConnection.Open();
                    mySQLDataReader = mySQLCommand.ExecuteReader();

                    //Create a class to hold your DB properties
                    ClassName classInstance = new ClassName();
                    while (mySQLDataReader.Read())
                    {
                        classInstance.Value = Convert.ToInt32(mySQLDataReader["DBColumnNameHoldingValue"]);                                           
                    }

                if(classInstance.Value == 1 && dt.Count == 1)
                {
                  //Display one Image then follow the same procedure for the other conditions
                }
             }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM