Silverlight之我見——資料批示(1)

來源:互聯網
上載者:User

 

第一次聽到這個概念,你是否有點陌生?MSDN上也沒有特意的去說明。不要看到這個名詞不太熟悉,其實資料批示,玩過C#的人都會非常熟悉,所謂資料批示,其本質就是特性(Attribute),怎麼樣,現在有點似曾相識了吧?  Attribute可以附加在命名空間、類,以及成員定義上的一種“特殊描述”,如下所示,這種標誌枚舉相信大家在使用COM互通性或者引入平台API的時候用得很多了。 

特性在使用的時候,可以忽略“Attribute”,如上面的,可以寫成Flags。

Silverlight(銀光)中的“資料批示”概念現在不陌生了,那麼,它為何要叫資料批示呢?

因為這些屬性類別都是用於定義實體類的中繼資料的,它很像SQL裡面的欄位屬性,如是否為只能,是否為自增長,是否為主/外鍵等。

這些類都定義在System.ComponentModel.DataAnnotations命名空間裡面,有興趣的可以查閱MSDN,這裡當然不會逐個列舉,我們只挑常用的來討論。

 

好的,今天我們討論第一個,相信也是使用頻率最高的——DisplayAttribute。

Name屬性:在UI中顯示欄位標籤,下面看了樣本你就明白了。

Description:對欄位(屬性)的描述,可以在UI中向使用者顯示工具提示資訊。

Order:欄位在使用者介面的顯示順序,這個不用介紹了,和以前的ListView或DataGridView類似(System.Windows.Forms中)。

 

OK,就這幾個,其實的屬性不那麼重要,其實使用Name和Description就足夠了,來,看看下面這個實體類(實體類這玩意兒嘛,你就理解為對客觀事物的一種抽象,相當於資料庫中的表,用E-R圖畫出來可能生動一點)。

    public class Song    {        string m_Name = "";        string m_Singer = "";         public Song(string songName, string singer)        {            this.m_Name = songName;            this.m_Singer = singer;        }         [Display(Name = "歌曲名", Description = "請輸入歌曲名。")]        public string Name        {            get { return this.m_Name; }            set { this.m_Name = value; }        }         [Display(Name = "歌手", Description = "請輸入歌手姓名。")]        public string Singer        {            get { return this.m_Singer; }            set { this.m_Singer = value; }        }    } 

這是一個歌曲類,它有兩個屬性:歌名和歌手,在上面的代碼中,你應該看到了DisPlayAttribute的用法了。

但你一定有些迷惑,不要緊,所見即所得 (WYSIWYG),運行程式一看便知曉。

 

上面用到了Label控制項,這個控制項不在.NET類庫中定義,它在SDK的System.Windows.Controls.Data.Input.dll中,所以,使用前一定要把它添加到項目的引用列表中,這個就不用說了,玩VS的人都知道,省去38個字。

 

好,看看上面的,發現了沒?Label上顯示的,正是我們剛才定義的DisPlayAttrbute的Name屬性。

 我們把兩個TextBox分別綁定到Name和Singer屬性。

你一定發現,在文字框的右側有一個像“i”的符號,然後你把滑鼠移到上面,別動,你就看到那幾個字,記得嗎?這幾個字在哪裡定義的?對了,就是DisPlayAttribute的Description屬性。

 

現在,你感悟了沒有?那麼,Label是如何綁定起來的呢?

把Target設定為要綁定的控制項名就行了,如這裡是綁定到文字框,因為綁定路徑不複雜,所以,無需設定屬性路徑。

好了,現在我就把XAML放出來,亮亮相。

大家不妨自己動手試試,很有意思的。

<UserControl x:Class="資料批註樣本.MainPage"    xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation "     xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml "    xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 " xmlns:mc=" http://schemas.openxmlformats.org/markup-compatibility/2006 "     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"    xmlns:sdk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input">  <Grid x:Name="LayoutRoot">        <Grid.ColumnDefinitions>            <ColumnDefinition Width="auto"/>            <ColumnDefinition Width="*"/>        </Grid.ColumnDefinitions>        <Grid.RowDefinitions>            <RowDefinition Height="27"/>            <RowDefinition Height="27"/>        </Grid.RowDefinitions>        <sdk:Label x:Name="lbName" Target="{Binding ElementName=txtName}" Grid.Column="0"                 Grid.Row="0" FontSize="14" Margin="1,1,20,1"/>        <sdk:Label x:Name="lbSinger" Target="{Binding ElementName=txtSinger}" Grid.Column="0"                 Grid.Row="1" FontSize="14" Margin="1,1,20,1"/>        <StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal">            <TextBox x:Name="txtName" Margin="1,1" Width="165"                    Text="{Binding Name}"/>            <sdk:DescriptionViewer Target="{Binding ElementName=txtName}" />        </StackPanel>        <StackPanel Grid.Column="1" Grid.Row="1" Orientation="Horizontal">            <TextBox x:Name="txtSinger" Margin="1,1" Width="165"                     Text="{Binding Singer}"/>            <sdk:DescriptionViewer Target="{Binding ElementName=txtSinger}"/>        </StackPanel>    </Grid></UserControl>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.