欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Silverlight动态创建Gird

发布时间:2024/9/20 编程问答 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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程序的某些时候难免要动态创建控件或修改控件事件,以下为示例代码: Code
 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 
参考二:
  • <UserControl x:Class="Sample.dragrect"
  •     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  •     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  •     Width="780" Height="400">
  •     <StackPanel Background="Green" 
  •                 Orientation="Horizontal">
  •         <Canvas x:Name="LayoutRoot"
  •                 Background="GreenYellow"
  •                 Width="650" Height="400"
  •                 MouseMove="Canvas_MouseMove"
  •                 MouseLeftButtonDown="Canvas_MouseLeftButtonDown" 
  •                 MouseLeftButtonUp="Canvas_MouseLeftButtonUp"/>
  •         <StackPanel Background="Gold" Margin="10">
  •             <TextBlock Text="选择颜色:"/>
  •             <Button x:Name="btnRed" 
  •                     Width="100" Height="50" 
  •                     FontSize="20" Content="Red" Margin="5" 
  •                     Click="btnRed_Click"/>
  •             <Button x:Name="btnBlue" 
  •                     Width="100" Height="50"
  •                     FontSize="20" Content="Blue" Margin="5" 
  •                     Click="btnBlue_Click"/>
  •             <Button x:Name="btnGreen" 
  •                     Width="100" Height="50"
  •                     FontSize="20" Content="Green" Margin="5"
  •                     Click="btnGreen_Click"/>
  •             <Button x:Name="btnClear" 
  •                     Width="100" Height="50"
  •                     FontSize="20" Content="Clear" Margin="5" 
  •                     Background="Red"
  •                     Click="btnClear_Click"/>
  •         </StackPanel>
  •     </StackPanel>
  • </UserControl>
  • C#代码:

  • using System;
  • using System.Collections.Generic;
  • using System.Linq;
  • using System.Net;
  • 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 Sample
  • {
  •     public partial class dragrect : UserControl
  •     {
  •         public dragrect()
  •         {
  •             InitializeComponent();
  •         }
  •         bool mouseMoveing = false;
  •         Point mousePoint;
  •         Color rectColor = Colors.Red;
  •         private void Canvas_MouseMove(object sender, MouseEventArgs e)
  •         {
  •             //如果鼠标没有拖动矩形则返回
  •             if (!mouseMoveing)
  •                 return;
  •             //获取鼠标当前坐标
  •             Point curPos = e.GetPosition(null);
  •             //取得最小坐标值
  •             double posX = mousePoint.X;
  •             double posY = mousePoint.Y;
  •             //计算矩形的宽和高
  •             double rectWidth = Math.Abs(curPos.X - mousePoint.X);
  •             double rectHeight = Math.Abs(curPos.Y - mousePoint.Y);
  •             //创建一个矩形元素
  •             Rectangle rect = new Rectangle();
  •             //声明矩形的宽和高
  •             rect.Width = rectWidth;
  •             rect.Height = rectHeight;
  •             //填充颜色
  •             rect.Fill = new SolidColorBrush(rectColor);
  •             //声明矩形在Canvas中创建的位置
  •             Canvas.SetLeft(rect, posX);
  •             Canvas.SetTop(rect, posY);
  •             //添加矩形到Canvas中
  •             LayoutRoot.Children.Add(rect);
  •         }
  •         private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  •         {
  •             //获取当前的鼠标位置
  •             mousePoint = e.GetPosition(null);
  •             //开始创建矩形
  •             mouseMoveing = true;
  •         }
  •         private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  •         {
  •             //矩形创建完成
  •             mouseMoveing = false;
  •         }
  •         private void btnRed_Click(object sender, RoutedEventArgs e)
  •         {
  •             //声明矩形颜色为Red
  •             rectColor = Colors.Red;
  •         }
  •         private void btnBlue_Click(object sender, RoutedEventArgs e)
  •         {
  •             //声明矩形颜色为Blue
  •             rectColor = Colors.Blue;
  •         }
  •         private void btnGreen_Click(object sender, RoutedEventArgs e)
  •         {
  •             //声明矩形颜色为Green
  •             rectColor = Colors.Green;
  •         }
  •         private void btnClear_Click(object sender, RoutedEventArgs e)
  •         {
  •             //清除所有Canvas内的矩形元素
  •             LayoutRoot.Children.Clear();
  •         }
  •     }
  • }
  • 参考三:

    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);

     

    与50位技术专家面对面20年技术见证,附赠技术全景图

    总结

    以上是生活随笔为你收集整理的Silverlight动态创建Gird的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。