1

Hello stackoverflow community,

I am having troubles getting buttons and my labels to align correctly, I was wondering if you guys to give me some assistance! Thanks!

Screenshot

enter image description here

As you can see the buttons are a bit off aligned.

Code

// Testing Purposes
        List<Item> headers = TestingClass.getHeaders();

        // Organization of the grid structure
        RowDefinition r1 = new RowDefinition();
        grid_headers.RowDefinitions.Add(r1);

        // Header for Label
        ColumnDefinition cd0 = new ColumnDefinition();
        Label label = new Label();
        label.Content = "Label";
        Grid.SetColumn(label, 0);
        grid_headers.ColumnDefinitions.Add(cd0);
        grid_headers.Children.Add(label);

        // Header for Description
        ColumnDefinition cd1 = new ColumnDefinition();
        Label description = new Label();
        Grid.SetColumn(description, 1);
        description.Content = "Description";
        grid_headers.ColumnDefinitions.Add(cd1);
        grid_headers.Children.Add(description);

        // Header for selection buttons
        ColumnDefinition cd2 = new ColumnDefinition();
        Label selection = new Label();
        Grid.SetColumn(selection, 2);
        selection.Content = "Selction";
        grid_headers.ColumnDefinitions.Add(cd2);
        grid_headers.Children.Add(selection);

        // Loop through all of the headers
        for (int i = 0; i < headers.Count; i++)
        {
            // Create button for choosing the header
            Button btn = new Button();
            btn.Content = "Select";
            btn.Name = headers[i].Label;
            btn.Width = 50;
            btn.Height = 20;
            btn.Padding = new Thickness(0.0, 0.0, 0.0, 0.0);
            btn.Click += selectHeader;

            RowDefinition rd = new RowDefinition();
            grid_headers.RowDefinitions.Add(rd);

            // Header for the labels
            Label label_header = new Label();
            label_header.Content = headers[i].Label;
            Grid.SetColumn(label_header, 0);
            Grid.SetRow(label_header, i+1); // Will always be i + 1 due to the first row for the main labels
            grid_headers.Children.Add(label_header);

            // Headers for the description
            Label description_header = new Label();
            description_header.Content = headers[i].Description;
            Grid.SetColumn(description_header, 1);
            Grid.SetRow(description_header, i + 1); // Will always be i + 1 due to the first row for the main labels
            grid_headers.Children.Add(description_header);

            // Button for selecting which header
            Grid.SetColumn(btn, 2);
            Grid.SetRow(btn, i + 1); // Will always be i + 1 due to the first row for the main labels
            grid_headers.Children.Add(btn);
        }

In my code, I generated the grid dynamically.
Thank you for the assistance I really appreciate it.

AustinT
  • 1,998
  • 8
  • 40
  • 63
  • Don't create or manipulate UI elements in procedural code in WPF. That's what XAML is for. – Federico Berasategui Aug 30 '13 at 21:30
  • @HighCore Thank you for your advice. I understand I would rather do it in XAML, but I generate the UI elements dynamically from a database. What do you feel the best way to accomplish this is with WPF? – AustinT Aug 30 '13 at 21:33
  • 2
    Either use a `ListView` or another type of `ItemsControl`. your current approach is far from ideal. – Federico Berasategui Aug 30 '13 at 21:35
  • Are you able to have button's in ListViews? – AustinT Aug 30 '13 at 21:36
  • @AustinTroung [you can put anything inside anything in WPF](http://stackoverflow.com/q/15532639/643085) (as opposed to ancient UI frameworks). [this](http://stackoverflow.com/a/15580293/643085) for example, is a `ListBox`. – Federico Berasategui Aug 30 '13 at 21:38
  • Thank you, could you please point me in a direction? Which should I use? : Gridview, GridData, etc.... Thank you for the help I appreciate it. – AustinT Aug 30 '13 at 21:41
  • Take a look at `DataGrid` as well. This looks like it could easily fit into it. – jamesSampica Aug 30 '13 at 21:47
  • Thanks @Jim I attempted to use DataGrid before, but had troubles adding button's to datagrids. Or at least I want the ability to select a row. – AustinT Aug 30 '13 at 21:47
  • 2
    Datagrids support selectable rows by default and buttons are easy to add. I'd say give it another shot, edit your question with your datagrid code and let us help. – jamesSampica Aug 30 '13 at 21:50
  • Thank you jim I really appreciate it, I will give it a shot – AustinT Aug 30 '13 at 21:53

0 Answers0