日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Silverlight动态创建Gird

發(fā)布時間:2024/9/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Silverlight动态创建Gird 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
用DataGrid有時候無法定制非常個性化的界面,Grid就相當于html中的table了。
按道理可以定制任何想要的界面。如圖

大氣象 ?void?GeneralGrid()
{
????Grid?grid?
=?new?Grid();
????
//定義兩行
????grid.RowDefinitions.Insert(0,?new?RowDefinition()?{?Height?=?new?GridLength(20)?});
????grid.RowDefinitions.Insert(
1,?new?RowDefinition()?{?Height?=?new?GridLength(20)?});
????
//定義兩列
????grid.ColumnDefinitions.Insert(0,?new?ColumnDefinition()?{?Width?=?new?GridLength(50)?});
????grid.ColumnDefinitions.Insert(
1,?new?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里面的動態(tài)控件
????grid.Children.Add(txt);
????grid.Children.Add(btn);

????
//設置Grid的外邊距
????grid.Margin?=?new?Thickness(10,?20,?30,?40);//左上右下,順時針

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

?

參考一:silverlight動態(tài)創(chuàng)建控件及控件事件動態(tài)指定(c#)
在寫silverlight程序的某些時候難免要動態(tài)創(chuàng)建控件或修改控件事件,以下為示例代碼: 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按鈕單擊后,執(zhí)行方法?
14?????????void?Another_Click(object?sender,?RoutedEventArgs?e)?
15?????????{?
16?????????????//創(chuàng)建一個Button?
17?????????????Button?b?=?new?Button();?
18?????????????//顯示內容?
19?????????????b.Content?=?"I?live!";?
20?????????????//為新創(chuàng)建的控件新建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?????????//點擊添加的控件觸發(fā)?
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);
  • ????????????//創(chuàng)建一個矩形元素
  • ????????????Rectangle?rect?=?new?Rectangle();
  • ????????????//聲明矩形的寬和高
  • ????????????rect.Width?=?rectWidth;
  • ????????????rect.Height?=?rectHeight;
  • ????????????//填充顏色
  • ????????????rect.Fill?=?new?SolidColorBrush(rectColor);
  • ????????????//聲明矩形在Canvas中創(chuàng)建的位置
  • ????????????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);
  • ????????????//開始創(chuàng)建矩形
  • ????????????mouseMoveing?=?true;
  • ????????}
  • ????????private?void?Canvas_MouseLeftButtonUp(object?sender,?MouseButtonEventArgs?e)
  • ????????{
  • ????????????//矩形創(chuàng)建完成
  • ????????????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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。