在Stackpanel中对动态用户控件进行排序

时间:2017-07-20 00:37:24

标签: c# wpf sorting controls stackpanel

我有一个用户控件,其中包含多个包含值的标签,例如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();
    }
}

This is a photo of how the Stackpanel looks

1 个答案:

答案 0 :(得分:0)

怎么样

string Query = "select * from Items orderby UploadDate";
相关问题