我有一个用户控件,其中包含多个包含值的标签,例如Uploaded的日期,Title的字符串,Author的字符串等。 截至目前,我通过为SQL数据库中的每一行数据创建一个用户控件的新实例来填充堆栈面板。
我想要的是点击"按日期排序"按钮,并使用户控件对象的所有实例按堆栈面板中的上载日期排序。
我将如何处理此事?
使用用户控件的新实例填充stackpanel的一些代码。
/// <summary>
/// Receive data from SQL and apply them to a new instance of itemControl
/// </summary>
public static void GetResultItems(StackPanel stackPanel)
{
// The SQL Query
string Query = "select * from Items";
MySqlCommand command = new MySqlCommand(Query, connection);
// Open connection
connection.Open();
MySqlDataReader reader = command.ExecuteReader();
// While reading...
while (reader.Read())
{
// Creates new instance of the itemControl
ItemControl itemControl = new ItemControl();
// Assign the data from the SQL database onto the itemControl labels
//Upload Date. This contains the date for when it was Uploaded.
itemControl.lblListUploaded.Content = (reader["UploadDate"].ToString()); //I wish to use this value to sort by
//Author
itemControl.lblExpandAuthor.Content = "Author: " + (reader["Author"].ToString());
// Add the new instance of ItemControl to the resultItem List<>
//The list is never used, but is created incase of later usage/need.
resultItem.Add(itemControl);
// Add the item to the Stackpanel
stackPanel.Children.Add(itemControl);
}
// Close connection
connection.Close();
}
}
答案 0 :(得分:0)
怎么样
string Query = "select * from Items orderby UploadDate";