C# 将按钮使用网格与wpf对齐的困难
大家好,我的社区 我很难让按钮和标签正确对齐,我想知道你们是否能给我一些帮助!谢谢 截图 正如你所看到的,按钮有点不对齐 密码 在我的代码中,我动态生成了网格。C# 将按钮使用网格与wpf对齐的困难,c#,wpf,C#,Wpf,大家好,我的社区 我很难让按钮和标签正确对齐,我想知道你们是否能给我一些帮助!谢谢 截图 正如你所看到的,按钮有点不对齐 密码 在我的代码中,我动态生成了网格。 感谢您的帮助,我非常感谢。不要在WPF的过程代码中创建或操作UI元素。这就是XAML的作用。@HighCore感谢您的建议。我知道我更愿意在XAML中实现,但我从数据库动态生成UI元素。您认为使用WPF实现这一点的最佳方法是什么?使用ListView或其他类型的ItemsControl。你目前的方法还远远不够理想。好的,谢谢。我很感激它
感谢您的帮助,我非常感谢。不要在WPF的过程代码中创建或操作UI元素。这就是XAML的作用。@HighCore感谢您的建议。我知道我更愿意在XAML中实现,但我从数据库动态生成UI元素。您认为使用WPF实现这一点的最佳方法是什么?使用ListView或其他类型的ItemsControl。你目前的方法还远远不够理想。好的,谢谢。我很感激它。默认情况下,DataGrids支持可选行,并且按钮很容易添加。我会说再给它一次机会,用你的datagrid代码编辑你的问题,让我们来帮助你。
// 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);
}