D
D
Danya Kozlovskiy2021-05-26 02:41:18
WPF
Danya Kozlovskiy, 2021-05-26 02:41:18

How to dynamically display data from a file?

Hello, there is data in the file that I read and try to display in the window, but the data is not displayed. How to make this hard worker work?
PS: the "=" sign is used as a separator between records.

public partial class recordsTable : Window
    {
        public recordsTable()
        {
            ShowRecords();
            InitializeComponent();
        }

        public static class MyStackPanel
        {
            public static StackPanel stackPanel2;

            public static void CreateStackPanel()
            {
                StackPanel stackPanel = new StackPanel();
                stackPanel.Orientation = System.Windows.Controls.Orientation.Horizontal;
                stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
                stackPanel.VerticalAlignment = VerticalAlignment.Top;
                stackPanel2 = stackPanel;
            }

            public static void AddInStackPanel(Border item)
            {
                Debug.Write(item);
                Debug.WriteLine("ITEM", item);
                stackPanel2.Children.Add(item);
            }
        }

        private void ShowRecords()
        {
            int i = 0;
            bool isNumberPosition = false;
            string path = AppDomain.CurrentDomain.BaseDirectory + "/Resources/records.txt";
            using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if(line == "=")
                    {
                        MyStackPanel.CreateStackPanel();
                        isNumberPosition = true;
                        if(i > 0)
                        {
                            RootStackPanel.Children.Add(MyStackPanel.stackPanel2);
                        }
                        i++;
                        continue;
                    }
                    if(isNumberPosition)
                    {
                        MyStackPanel.AddInStackPanel((CreateLabel(line, 40)));
                        isNumberPosition = false;
                    }
                    else
                    {
                        MyStackPanel.AddInStackPanel((CreateLabel(line)));
                    }
                }
            }
        }



        private Border CreateLabel(string value, int width = 150)
        {
            Border border = new Border();
            Label label = new Label();
            border.BorderBrush = Brushes.Gray;
            border.BorderThickness = new Thickness(2);

            label.FontSize = 16;
            label.Width = width;
            label.Content = value;

            border.Child = label;
            Debug.Write(label);
            Debug.WriteLine("LABEL", label);
            return border;
        }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question