在 Stackpanel 中对动态用户控件进行排序
Sorting Dynamic User controls in Stackpanel
我有一个用户控件,它有多个包含值的标签,例如上传日期、标题字符串、作者字符串等。
截至目前,我正在通过为 SQL 数据库中的每一行数据创建一个新的用户控件实例来填充堆栈面板。
我想要的是单击 "Sort by Date" 按钮并让用户控件的所有实例 object 按 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();
}
}
This is a photo of how the Stackpanel looks
怎么样
string Query = "select * from Items orderby UploadDate";
我有一个用户控件,它有多个包含值的标签,例如上传日期、标题字符串、作者字符串等。 截至目前,我正在通过为 SQL 数据库中的每一行数据创建一个新的用户控件实例来填充堆栈面板。
我想要的是单击 "Sort by Date" 按钮并让用户控件的所有实例 object 按 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();
}
}
This is a photo of how the Stackpanel looks
怎么样
string Query = "select * from Items orderby UploadDate";