利用C#動態產生Xaml

來源:互聯網
上載者:User

    之前在某一WPF交流群中一群友提問關於xaml轉c#的工具,目前好像沒有這方面的程式。忽而發覺在某些時候需要用C#動態產生xaml。例如在需要產生大量元素的時候,難道需要一個個的去寫嗎?這時用C#代碼去寫便比較高效率了。但是在一般情況下,是不需要用C#代碼去寫,用xaml效率反而更高。

    舉一個代碼例子吧,
xmal:

<Window x:Class="WpfApplication9.Window1"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Window1" Height="300" Width="300"></Window>

可以看到,Window1中沒有任何容器和控制項。

我們現在往裡面加上一個三行一列的Grid容器:

Window1的建構函式中:

 

InitializeComponent();            Grid grid = new Grid();    //執行個體化一個Grid            RowDefinition row1= new RowDefinition();   //執行個體化一個Grid行            RowDefinition row2 = new RowDefinition();              RowDefinition row3 = new RowDefinition();            ColumnDefinition col = new ColumnDefinition();    //執行個體化一個Grid列            row1.Height = new GridLength(3, GridUnitType.Star);   //設定行的高度(按比例)            row2.Height = new GridLength(4, GridUnitType.Star);            row3.Height = new GridLength(3, GridUnitType.Star);
         col.Width = new GridLength(10,GridUnitType.Star);     //設定列的寬度(完全填充)             //row3.Height = new GridLength(51, GridUnitType.Pixel);   //設定行的高度(按像素)            //row3.Height = GridLength.Auto;                          //設定行的高度(根據元素的高度來確定)

         grid.RowDefinitions.Add(row1);     //添加到grid的行集合中            grid.RowDefinitions.Add(row2);            grid.RowDefinitions.Add(row3);            grid.ColumnDefinitions.Add(col);    //添加到grid 的列集合中            this.Content = grid;             //設定grid為表單的主容器

現在就將Grid容器構造完畢,並添加到了Window1中。

接下來添加幾個控制項進去:

         TextBlock text = new TextBlock();            text.Text = "這是一個樣本!!(我佔3份)";            Button button1 = new Button();            button1.Width = button1.Height = 50;            Button button2 = new Button();            button1.Content = "按鈕1(我佔4份)";            button2.Content = "按鈕2(我佔3份)";            grid.Children.Add(button1);            grid.Children.Add(button2);            grid.Children.Add(text);

現在若運行下,會發現所有控制項都擠在了第一行中,因為我們上一步只是添加,還沒有設定,現在我們設定一下:

         Grid.SetRow(text, 0);        //設定text為第一行              Grid.SetRow(button1, 1);              Grid.SetRow(button2, 2);             //Grid.SetColumn(text,0);  //設定text為第一列

到目前為止就已經寫好一個介面了,是不是很繁瑣?

 

好吧,我也是這麼認為的,但是如果你要這樣寫:

Grid grid = new Grid();                for (int g = 0; g <= 255; g  )                {                    for (int b = 0; b <= 255; b  )                    {                            if (grid.RowDefinitions.Count <= 255)                            {                                RowDefinition row = new RowDefinition();                                row.Height = GridLength.Auto;                                grid.RowDefinitions.Add(row);                            }                                                         if(grid.RowDefinitions.Count <= 255)                            {                                ColumnDefinition column = new ColumnDefinition();                                column.Width = GridLength.Auto;                                grid.ColumnDefinitions.Add(column);                            }                             Color color = new Color();                             color.A = Convert.ToByte(g);                             color.R = 255;                             color.G =0;                             color.B = Convert.ToByte(b);                           // Debug.WriteLine(color.ToString());                             Border border = new Border();                             border.BorderBrush = new SolidColorBrush(Colors.Black);                             border.Background = new SolidColorBrush(color);                             border.Margin = new Thickness(2);                                                        TextBlock texttool = new TextBlock();                            texttool.Text = color.ToString();                            border.ToolTip = texttool;                                                          border.Width = border.Height = 10;                                                        Grid.SetRow(border, b);                            Grid.SetColumn(border, g);                            grid.Children.Add(border);                         }                }            this.Content = grid;

設定Border的背景分別為256*255種顏色,難道256*255個Border、255個列和255個行要手動寫嗎?自然用代碼效率是高的。

 

本文章只是簡單的提一下基礎的東西,因此不是很詳細。

 

至於那個神馬Xmal轉C#的程式,就有望大神們開發了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.