生活随笔
收集整理的這篇文章主要介紹了
Silverlight动态创建Gird
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用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里面的動態控件
????grid.Children.Add(txt);
????grid.Children.Add(btn);
????//設置Grid的外邊距
????grid.Margin?=?new?Thickness(10,?20,?30,?40);//左上右下,順時針
????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.SetLeft(rect,?posX);????????????Canvas.SetTop(rect,?posY);????????????????????????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)????????{????????????????????????rectColor?=?Colors.Red;????????}????????private?void?btnBlue_Click(object?sender,?RoutedEventArgs?e)????????{????????????????????????rectColor?=?Colors.Blue;????????}????????private?void?btnGreen_Click(object?sender,?RoutedEventArgs?e)????????{????????????????????????rectColor?=?Colors.Green;????????}????????private?void?btnClear_Click(object?sender,?RoutedEventArgs?e)????????{????????????????????????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的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。