WPF中如何动态写出XAML界面,如图。

图片说明

请问红色框框里面的该怎么用C#写,而不是XAML代码。用thickness老控制不好。

可以做成usercontrol,样式提前写好,直接new 加载样式就好了

XAML:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DynDemo" Height="300" Width="500">





    </StackPanel>
    </StackPanel>
</Grid>

CS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfAppDemo
{
///
/// DynDemo.xaml 的交互逻辑
///
public partial class DynDemo : Window
{
public DynDemo()
{
InitializeComponent();

        Label lab1 = new Label();
        lab1.Content = "帐号:";
        canvas.Children.Add(lab1);
        lab1.Width = 40;
        lab1.Height = 30;

        TextBox txt1 = new TextBox();
        txt1.Text = "";
        canvas.Children.Add(txt1);
        canvas.RegisterName("txt1", txt1);//注册名字,以便以后使用 
        txt1.Width = 100;
        txt1.Height = 20;

        Label lab2 = new Label();
        lab2.Content = "密码:";
        canvas.Children.Add(lab2);

        lab2.Width = 40;
        lab2.Height = 30;

        TextBox txt2= new TextBox();
        txt2.Text = "";
        canvas.Children.Add(txt2);
        txt2.Width = 100;
        txt2.Height = 20;

        Label lab3 = new Label();
        lab3.Content = "确认密码:";
        canvas.Children.Add(lab3);

        lab3.Width = 60;
        lab3.Height = 30;

        TextBox txt3 = new TextBox();
        txt3.Text = "";
        canvas.Children.Add(txt3);
        txt3.Width = 100;
        txt3.Height = 20;




    }
}

}

图片说明