Wpf列重叠/未显示

本文关键字:显示 重叠 Wpf | 更新日期: 2023-09-27 18:15:44

我的Grid中的列只显示第二列(文本字段)(而不是标签),我在这里哪里出错了?

StackPanel sp = new StackPanel();
Grid grid = new Grid();
ScrollViewer viewer = new ScrollViewer();
Label lbl_Doc = new Label();
Label lbl_ApplicationId = new Label();
TextBox txt_ApplicationId = new TextBox();
Label lbl_ClientId = new Label();
Label lbl_ClientSecret = new Label();
TextBox txt_ClientId = new TextBox();
TextBox txt_ClientSecret = new TextBox();
Button btn_GetAuthorization = new Button();
WebBrowser webbrowser_webbrowser = new WebBrowser()
public Settings()
{
    InitializeComponent();
    // add Grid          
    //grid.Margin = new Thickness(0,0,-76.5,79.5);
    // row 1
    RowDefinition rd1 = new RowDefinition();
    rd1.Height = new GridLength(15, GridUnitType.Auto);
    grid.RowDefinitions.Add(rd1);
    // row 2
    RowDefinition rd2 = new RowDefinition();
    rd2.Height = new GridLength(15,GridUnitType.Auto);
    grid.RowDefinitions.Add(rd2);
    // row 3
    RowDefinition rd3 = new RowDefinition();
    rd3.Height = new GridLength(15,GridUnitType.Auto);
    grid.RowDefinitions.Add(rd3);
    // row 4
    RowDefinition rd4 = new RowDefinition();
    rd4.Height = new GridLength(15, GridUnitType.Auto);
    grid.RowDefinitions.Add(rd4);
    // row 5
    RowDefinition rd5 = new RowDefinition();
    rd5.Height = new GridLength(15, GridUnitType.Auto);
    grid.RowDefinitions.Add(rd5);
    // column 1
    ColumnDefinition cd1 = new ColumnDefinition();
    cd1.Width = new GridLength(500, GridUnitType.Auto);
    grid.ColumnDefinitions.Add(cd1);
    // column 2
    ColumnDefinition cd2 = new ColumnDefinition();
    cd2.Width = new GridLength(250, GridUnitType.Auto);
    grid.ColumnDefinitions.Add(cd2);
    // Add Documentation           
    lbl_Doc.Content = "Enter values:";
    Grid.SetRow(lbl_Doc, 0);
    grid.Children.Add(lbl_Doc);
    // Add label + input for Google Application Id            
    lbl_ApplicationId.Content = "Google Application Id";
    lbl_ApplicationId.HorizontalAlignment = HorizontalAlignment.Left;
    //lbl_ApplicationId.VerticalAlignment = VerticalAlignment.Top;
    lbl_ApplicationId.Width = 150;
    //lbl_ApplicationId.Margin = new Thickness(5,35,0,0);
    Grid.SetRow(lbl_ApplicationId, 1);
    Grid.SetColumn(lbl_ApplicationId, 1);
    grid.Children.Add(lbl_ApplicationId);
    //txt_ApplicationId.HorizontalAlignment = HorizontalAlignment.Left;
    //txt_ApplicationId.VerticalAlignment = VerticalAlignment.Top;
    txt_ApplicationId.Height = 23;
    txt_ApplicationId.Width = 420;
    //txt_ApplicationId.Margin = new Thickness(160,36,0,0);
    txt_ApplicationId.FontSize = 10;
    txt_ApplicationId.TextWrapping = TextWrapping.Wrap;
    txt_ApplicationId.Text = init_application_id;
    txt_ApplicationId.TextChanged +=txt_TextChanged;
    Grid.SetRow(txt_ApplicationId, 1);
    Grid.SetColumn(txt_ApplicationId, 2);
    grid.Children.Add(txt_ApplicationId);
    // Add label + input for Google Client Id           
    lbl_ClientId.Content = "Google Client Id";
    //lbl_ClientId.HorizontalAlignment = HorizontalAlignment.Left;
    //lbl_ClientId.VerticalAlignment = VerticalAlignment.Top;
    lbl_ClientId.Width = 150;
    //lbl_ClientId.Margin = new Thickness(5, 35, 0, 0);
    Grid.SetRow(lbl_ClientId, 2);
    Grid.SetColumn(lbl_ClientId, 1);
    grid.Children.Add(lbl_ClientId);
    //txt_ClientId.HorizontalAlignment = HorizontalAlignment.Left;
    //txt_ClientId.VerticalAlignment = VerticalAlignment.Top;
    txt_ClientId.Height = 23;
    txt_ClientId.Width = 420;
    //txt_ClientId.Margin = new Thickness(160, 36, 0, 0);
    txt_ClientId.FontSize = 10;
    txt_ClientId.TextWrapping = TextWrapping.Wrap;
    txt_ClientId.TextChanged +=txt_TextChanged;
    txt_ClientId.Text = init_clientid;
    Grid.SetRow(txt_ClientId, 2);
    Grid.SetColumn(txt_ClientId, 2);
    grid.Children.Add(txt_ClientId);
    // Add label + input for Google Client Secret            
    lbl_ClientSecret.Content = "Google Client Secret";
    //lbl_ClientSecret.HorizontalAlignment = HorizontalAlignment.Left;
    //lbl_ClientSecret.VerticalAlignment = VerticalAlignment.Top;
    lbl_ClientSecret.Width = 150;
    //lbl_ClientSecret.Margin = new Thickness(5, 35, 0, 0);
    Grid.SetRow(lbl_ClientSecret, 3);
    Grid.SetColumn(lbl_ClientSecret, 1);
    grid.Children.Add(lbl_ClientSecret);
    //txt_ClientSecret.HorizontalAlignment = HorizontalAlignment.Left;
    //txt_ClientSecret.VerticalAlignment = VerticalAlignment.Top;
    txt_ClientSecret.Height = 23;
    txt_ClientSecret.Width = 420;
    //txt_ClientSecret.Margin = new Thickness(160, 36, 0, 0);
    txt_ClientSecret.FontSize = 10;
    txt_ClientSecret.TextWrapping = TextWrapping.Wrap;
    txt_ClientSecret.TextChanged +=txt_TextChanged;
    txt_ClientSecret.Text = init_clientSecret;
    Grid.SetRow(txt_ClientSecret, 3);
    Grid.SetColumn(txt_ClientSecret, 2);
    grid.Children.Add(txt_ClientSecret);
    // button to get authorization             
    btn_GetAuthorization.Content = "Authorize";
    //btn_GetAuthorization.HorizontalAlignment = HorizontalAlignment.Left;
    //btn_GetAuthorization.VerticalAlignment = VerticalAlignment.Top;
    btn_GetAuthorization.Height = 21;
    btn_GetAuthorization.Width = 72;
   // btn_GetAuthorization.Margin = new Thickness(160,120,0,0);
    btn_GetAuthorization.Visibility = Visibility.Hidden;
    btn_GetAuthorization.Click += btn_GetAuthorization_Click;
    Grid.SetRow(btn_GetAuthorization, 4);
    Grid.SetColumn(btn_GetAuthorization, 1);
    grid.Children.Add(btn_GetAuthorization);
    // Add Scrollviewer            
    viewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
    viewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
    viewer.HorizontalAlignment = HorizontalAlignment.Stretch;
    viewer.VerticalAlignment = VerticalAlignment.Stretch;
    viewer.Content = grid;
    Google_Settings.Content = viewer;
    Google_Settings.SizeToContent = SizeToContent.WidthAndHeight;
}

Wpf列重叠/未显示

问题是您使用的索引:

Grid.SetColumn(txt_ApplicationId, 2);

从零开始,取值必须是01,而不是12

但正确的做法是使用XAMLDataBinding !