Silverlight动态创建Gird

用DataGrid有时候无法定制非常个性化的界面,Grid就相当于html中的table了。
按道理可以定制任何想要的界面。如图

大气象
 void GeneralGrid()
{
    Grid grid 
= new Grid();
    
//定义两行
    grid.RowDefinitions.Insert(0new RowDefinition() { Height = new GridLength(20) });
    grid.RowDefinitions.Insert(
1new RowDefinition() { Height = new GridLength(20) });
    
//定义两列
    grid.ColumnDefinitions.Insert(0new ColumnDefinition() { Width = new GridLength(50) });
    grid.ColumnDefinitions.Insert(
1new ColumnDefinition() { Width = new GridLength(100) });

    TextBox txt 
= new TextBox();
    txt.SetValue(Grid.RowProperty, 
0);//定义所在行
    txt.SetValue(Grid.ColumnProperty, 0);//定义所在列

    Button btn 
= new Button();
    btn.Content 
= "大气象";
    btn.SetValue(Grid.RowProperty, 
1);
    btn.SetValue(Grid.ColumnProperty, 
1);

    
//添加Grid里面的动态控件
    grid.Children.Add(txt);
    grid.Children.Add(btn);

    
//设置Grid的外边距
    grid.Margin = new Thickness(10203040);//左上右下,顺时针

    spDataList.Children.Add(grid);
//将Grid添加到StackPanel中
}

 

参考一:silverlight动态创建控件及控件事件动态指定(c#)
在写silverlight程序的某些时候难免要动态创建控件或修改控件事件,以下为示例代码:

 1 
 2 public partial class EventDemo : UserControl 
 3     { 
 4         private int newButtonPosition = 100
 5   
 6         public EventDemo() 
 7         { 
 8             InitializeComponent(); 
 9             //Anthor按钮单击事件 
10             Another.Click += new RoutedEventHandler(Another_Click); 
11         } 
12   
13         //Anthor按钮单击后,执行方法 
14         void Another_Click(object sender, RoutedEventArgs e) 
15         { 
16             //创建一个Button 
17             Button b = new Button(); 
18             //显示内容 
19             b.Content = "I live!"
20             //为新创建的控件新建Thickness对象,用来设置Button控件的位置 
21             //原文中使用了Canvas.LeftProperty和Canvas.TopProperty 
22             Thickness tn = new Thickness(10,this.newButtonPosition,0,0); 
23             b.SetValue(Canvas.StyleProperty, tn); 
24             //到顶部的距离递增 
25             this.newButtonPosition += 30
26             b.Width = 100
27             b.Height = 20
28             //给这个新建的按钮的Click事件添加一个处理方法 
29             b.Click += new RoutedEventHandler(b_Click); 
30             //添加到父控件,并显示 
31             myCanvas.Children.Add(b); 
32         } 
33   
34         //点击添加的控件触发 
35         void b_Click(object sender, RoutedEventArgs e) 
36         { 
37             Button btn = sender as Button; 
38             btn.Content = "Don't do that!"
39             btn.IsEnabled = false
40         } 
41     }
42 
43 
参考二:
  1. <UserControl x:Class="Sample.dragrect"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     Width="780" Height="400">
  5.     <StackPanel Background="Green" 
  6.                 Orientation="Horizontal">
  7.         <Canvas x:Name="LayoutRoot"
  8.                 Background="GreenYellow"
  9.                 Width="650" Height="400"
  10.                 MouseMove="Canvas_MouseMove"
  11.                 MouseLeftButtonDown="Canvas_MouseLeftButtonDown" 
  12.                 MouseLeftButtonUp="Canvas_MouseLeftButtonUp"/>
  13.         <StackPanel Background="Gold" Margin="10">
  14.             <TextBlock Text="选择颜色:"/>
  15.             <Button x:Name="btnRed" 
  16.                     Width="100" Height="50" 
  17.                     FontSize="20" Content="Red" Margin="5" 
  18.                     Click="btnRed_Click"/>
  19.             <Button x:Name="btnBlue" 
  20.                     Width="100" Height="50"
  21.                     FontSize="20" Content="Blue" Margin="5" 
  22.                     Click="btnBlue_Click"/>
  23.             <Button x:Name="btnGreen" 
  24.                     Width="100" Height="50"
  25.                     FontSize="20" Content="Green" Margin="5"
  26.                     Click="btnGreen_Click"/>
  27.             <Button x:Name="btnClear" 
  28.                     Width="100" Height="50"
  29.                     FontSize="20" Content="Clear" Margin="5" 
  30.                     Background="Red"
  31.                     Click="btnClear_Click"/>
  32.         </StackPanel>
  33.     </StackPanel>
  34. </UserControl>

C#代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. namespace Sample
  13. {
  14.     public partial class dragrect : UserControl
  15.     {
  16.         public dragrect()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         bool mouseMoveing = false;
  21.         Point mousePoint;
  22.         Color rectColor = Colors.Red;
  23.         private void Canvas_MouseMove(object sender, MouseEventArgs e)
  24.         {
  25.             //如果鼠标没有拖动矩形则返回
  26.             if (!mouseMoveing)
  27.                 return;
  28.             //获取鼠标当前坐标
  29.             Point curPos = e.GetPosition(null);
  30.             //取得最小坐标值
  31.             double posX = mousePoint.X;
  32.             double posY = mousePoint.Y;
  33.             //计算矩形的宽和高
  34.             double rectWidth = Math.Abs(curPos.X - mousePoint.X);
  35.             double rectHeight = Math.Abs(curPos.Y - mousePoint.Y);
  36.             //创建一个矩形元素
  37.             Rectangle rect = new Rectangle();
  38.             //声明矩形的宽和高
  39.             rect.Width = rectWidth;
  40.             rect.Height = rectHeight;
  41.             //填充颜色
  42.             rect.Fill = new SolidColorBrush(rectColor);
  43.             //声明矩形在Canvas中创建的位置
  44.             Canvas.SetLeft(rect, posX);
  45.             Canvas.SetTop(rect, posY);
  46.             //添加矩形到Canvas中
  47.             LayoutRoot.Children.Add(rect);
  48.         }
  49.         private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  50.         {
  51.             //获取当前的鼠标位置
  52.             mousePoint = e.GetPosition(null);
  53.             //开始创建矩形
  54.             mouseMoveing = true;
  55.         }
  56.         private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  57.         {
  58.             //矩形创建完成
  59.             mouseMoveing = false;
  60.         }
  61.         private void btnRed_Click(object sender, RoutedEventArgs e)
  62.         {
  63.             //声明矩形颜色为Red
  64.             rectColor = Colors.Red;
  65.         }
  66.         private void btnBlue_Click(object sender, RoutedEventArgs e)
  67.         {
  68.             //声明矩形颜色为Blue
  69.             rectColor = Colors.Blue;
  70.         }
  71.         private void btnGreen_Click(object sender, RoutedEventArgs e)
  72.         {
  73.             //声明矩形颜色为Green
  74.             rectColor = Colors.Green;
  75.         }
  76.         private void btnClear_Click(object sender, RoutedEventArgs e)
  77.         {
  78.             //清除所有Canvas内的矩形元素
  79.             LayoutRoot.Children.Clear();
  80.         }
  81.     }
  82. }

参考三:

Code below can add item that contain a button and a textbox which are lay in a grid to combobox.

ComboBoxItem item = new ComboBoxItem();
            Grid grid = new Grid();
            grid.ColumnDefinitions.Insert(0, new ColumnDefinition() { Width = new GridLength(50) });
            grid.ColumnDefinitions.Insert(1, new ColumnDefinition() { Width = new GridLength(100) });

            Button MyButton = new Button();
            MyButton.SetValue(Grid.ColumnProperty, 0);
            MyButton.DataContext = "11";
            MyButton.Click+=new RoutedEventHandler(MyButton_Click);
            TextBox textbox = new TextBox();
            textbox.Name = "text";
            textbox.SetValue(Grid.ColumnProperty, 1);

            grid.Children.Add(MyButton);
            grid.Children.Add(textbox);

            item.Content = grid;

            Combobox1.Items.Add(item);

 

posted @ 2010-06-23 17:32  大气象  阅读(3354)  评论(8编辑  收藏  举报
http://www.tianqiweiqi.com