首页  ·  知识 ·  移动开发
Silverlight在页面上动态生成控件
网友    Wphone  编辑:dezai   图片来源:网络
Silverlight 在页面上动态生成控件
public partial class dynamicControl : UserControl
    {
        public dynamicControl()
        {
            InitializeComponent();
            Button b = new Button();
            b.Height = 23;
            b.Width = 100;
            b.HorizontalAlignment = HorizontalAlignment.Left;
            //b.Margin = new Thickness(0);
            b.Click += buttonClick;
            b.Content = DateTime.Now;
            b.Tag = "b button" + DateTime.Now.ToString();
            LayoutRoot.Children.Add(b);
        }
        private void buttonClick(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            if (b != null)
                MessageBox.Show(b.Tag.ToString());
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder xaml = new StringBuilder();
            xaml.Append("<TextBlock ");
            xaml.Append("xmlns=\"http://schemas.microsoft.com/client/2007\" ");
            xaml.Append(" FontWeight=\"Bold\" Text=\"动态创建XAML对象\" />");
            //创建TextBlock
            TextBlock tb = (TextBlock)XamlReader.Load(xaml.ToString());
            LayoutRoot.Children.Add(tb);
            xaml = new StringBuilder();
            xaml.Append("<Button ");
            xaml.Append("xmlns=\"http://schemas.microsoft.com/client/2007\" ");
            xaml.Append(" Content=\"a Button\" Name=\"btn1\" Tag=\"a tag\"/>");
            //创建TextBlock
            Button b = (Button)XamlReader.Load(xaml.ToString());
            b.Click += buttonClick;
            LayoutRoot.Children.Add(b);
        }
    }
本文作者:网友 来源:网络
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读