2 zee89757 zee89757 于 2015.07.16 09:27 提问

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

图片说明

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

3个回答

CSDNXIAOD
CSDNXIAOD   2015.07.16 21:42

WPF实现界面动态布局
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

weixin_30834361
weixin_30834361   2015.08.25 14:16

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

isaboy
isaboy   2015.09.08 09:54

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;




    }
}

}

图片说明

Csdn user default icon
上传中...
上传图片
插入图片