Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort stackpanel based on child elements?

Tags:

c#

linq

wpf

Is there a way to order stackpanels based on some of its child elements?

In the code behind I add some generic things like groupbox and textblock to a stackpanel, one of the textblocks information is DateTime from my webservice, I have tryed sorting via descending order using linq but the output is still the same.

So I was wondering if its possible to sort by one of the stackpanels child elements namely the textblock1.Text that holds 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);
        }
    }
like image 601
G Gr Avatar asked Sep 07 '26 08:09

G Gr


1 Answers

The order of display is totally defined by the order of calls to

MainArea1.Children.Add(stackPanel);

So, try something like

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

(And you really should be using Temlates here)

like image 186
Henk Holterman Avatar answered Sep 09 '26 21:09

Henk Holterman