Sort stackpanel based on child elements?

Is there a way to arrange stacked panels based on some of its children?

In the code behind, I add some common things, such as groupbox and textblock, on the stack, one of these text blocks is DateTime from my web service, I tried sorting in descending order using linq, but the result is still the same.

So, I was wondering if it can be sorted by one of the children of the stack panels, namely textblock1.Text, which contains the DateTime attribute?

        XDocument xDoc = XDocument.Load(uriGroups);
        var sortedXdoc = xDoc.Descendants("Student")
                       .OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value));


        foreach (var node in xDoc.Descendants("Student"))
        {

            GroupBox groupbox = new GroupBox();
            groupbox.Header = String.Format(node.Element("StudentID").Value);
            groupbox.Width = 100;
            groupbox.Height = 100;
            groupbox.Margin = new Thickness(1);

            TextBlock textBlock = new TextBlock();
            textBlock.Text = String.Format(node.Element("FirstName").Value + " " + (node.Element("LastName").Value));
            textBlock.TextAlignment = TextAlignment.Center;

            TextBlock textBlock1 = new TextBlock();
            textBlock1.Text = (DateTime.Parse(node.Element("TimeAdded").Value)).ToString("d");
            String.Format("{0:d/M/yyyy}", DateTime.Parse(node.Element("TimeAdded").Value));
            textBlock1.TextAlignment = TextAlignment.Center;
            textBlock1.VerticalAlignment = VerticalAlignment.Bottom;

            StackPanel stackPanel = new StackPanel();
            stackPanel.Children.Add(groupbox);

            stackPanel.Children.Add(textBlock);
            stackPanel.Children.Add(textBlock1);
            stackPanel.Margin = new Thickness(5);
            stackPanel.MouseEnter += new MouseEventHandler(stackpanel_MouseEnter);
            stackPanel.MouseLeave += new MouseEventHandler(stackpanel_MouseLeave);
            MainArea1.Children.Add(stackPanel);
        }
    }
+3
source share
1 answer

The display order is completely determined by the order of calls.

MainArea1.Children.Add(stackPanel);

So try something like

 foreach (var node in xDoc.Descendants("Student").OrderBy(e => ...))
 {
    ....
 }

(And you really should use Temlates here)

+2
source

All Articles