Silverligth动态控件例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Dynamic_Control_Creation
{
    public partial class Page : UserControl
    {
        private enum Platform  { Web, Desktop };
        private StackPanel sp;
        public Page()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Web.Checked += new RoutedEventHandler(RB_Checked);
            Desk.Checked += new RoutedEventHandler(RB_Checked);
        }

        void RB_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton rb = e.OriginalSource as RadioButton;
            if (rb.Content.ToString().ToUpper() == "WEB")
            {
                SetChoices(Platform.Web);
            }
            else
            {
                SetChoices(Platform.Desktop);
            }
        }

 

        private void SetChoices(Platform whichPlatform)
        {
            if (sp == null)
            {
                sp = new StackPanel();
                LayoutRoot.Children.Add(sp);
            }
            else
            {
                sp.Children.Clear();
            }
            sp.Orientation = Orientation.Horizontal;
            sp.SetValue(Grid.RowProperty, 1);
            sp.SetValue(Grid.ColumnProperty, 3);
           
            if (whichPlatform == Platform.Desktop)
            {
                CreateCheckBox("Winform", sp);
                CreateCheckBox("WPF", sp);
            }
            else
            {
                CreateCheckBox("AJAX", sp);
                CreateCheckBox("Silverlight", sp);
            }
          
        }

        private void CreateCheckBox(
            string txt,
            StackPanel thePanel)
        {
            CheckBox cb = new CheckBox();
            cb.Content = txt;
            cb.Height = 30;
            cb.Width = 90;
            cb.Checked += new RoutedEventHandler(cb_Checked);
            cb.Unchecked += new RoutedEventHandler(cb_Checked);
            thePanel.Children.Add(cb);
        }

 

        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = e.OriginalSource as CheckBox;
            if (cb != null)
            {
                if (cb.IsChecked == true)
                {
                    Messages.Items.Add(cb.Content.ToString() + " selected");
                }
                else
                {
                    Messages.Items.Add(cb.Content.ToString() + " cleared");
                }
            }
        }
    }
}

posted @ 2008-12-05 15:00  昕友软件开发  阅读(328)  评论(0编辑  收藏  举报
欢迎访问我的开源项目:xyIM企业即时通讯