大菜部落格園之處女作:silverlight 行為淺談1———–載入資料

來源:互聯網
上載者:User

大哥大嫂們,先聽小弟小小扯淡一下,算是對咱處女作的支援把...............

小弟工作兩年,至今仍覺的技術太差,來新公司就直接接觸了silverlight 4 ,根本就沒有silverlight 1/ 2/3 的基礎,以前只是做web 開發,winform都沒開發過具體的項目,都是自己做著玩。公司要開發新項目,決定用EF4   和 行為(汗....一個也沒接觸過)。

 

閑話少說,先來看效果

 

前提:小弟只是做了個Demo

具體怎麼建立 silverlight項目 等操作,請上Google查

datagrid  列定義

 <sdk:DataGrid.Columns>         
             <sdk:DataGridTextColumn Binding="{Binding Id}" Header="ID" Width="200"/>
             <sdk:DataGridTextColumn Binding="{Binding Name}" Header="姓名" Width="100" />
             <sdk:DataGridTextColumn Binding="{Binding ClassName}" Header="班級" Width="80" />
            </sdk:DataGrid.Columns>

 

 

  由於是Demo請原諒小弟懶散...哈哈...話說回來,誰不懶呀....(給自己找個借口)

    用vs 2010 建立silverlight類庫,然後用blend 4 開啟項目,在新添加的類庫中添加一個Actions 檔案(尾碼也是.cs ,感覺其實就是類),可以看到預設產生的程式碼:

    

       我們修改一下類名,繼承 TargetedTriggerAction<UIElement>,也是為了方便添加元素的load 等事件,如下:

    public class Student_Action : TargetedTriggerAction<UIElement>

       為了在介面載入的時候,讓datagrid 就載入資料和方便傳值等操作,所以定義了一系列的相依性屬性:

          #region  定義相依性屬性 也就是傳值
        /// <summary>
        /// 行為的命令,為了同一個行為執行不同操作
        /// </summary>
        public static readonly DependencyProperty CommandNameProperty =
           DependencyProperty.Register("CommandName", typeof(String), typeof(Student_Action), null);
        public String CommandName
        {
            get { return (String)GetValue(Student_Action.CommandNameProperty); }
            set { SetValue(Student_Action.CommandNameProperty, value); }
        }
        /// <summary>
        /// id
        /// </summary>
        public static readonly DependencyProperty IdProperty =
            DependencyProperty.Register("Id", typeof(String ), typeof(Student_Action), null);
        public String Id
        {
            get { return (String)GetValue(Student_Action.IdProperty); }
            set { SetValue(Student_Action.IdProperty, value); }
        }
        /// <summary>
        /// 姓名
        /// </summary>
        public static readonly DependencyProperty StudentNameProperty =
           DependencyProperty.Register("StudentName", typeof(String), typeof(Student_Action), null);
        public String StudentName
        {
            get { return (String)GetValue(Student_Action.StudentNameProperty); }
            set { SetValue(Student_Action.StudentNameProperty, value); }
        }
        /// <summary>
        /// 班級
        /// </summary>
        public static readonly DependencyProperty ClassNameProperty =
          DependencyProperty.Register("ClassName", typeof(String), typeof(Student_Action), null);
        public String ClassName
        {
            get { return (String)GetValue(Student_Action.ClassNameProperty); }
            set { SetValue(Student_Action.ClassNameProperty, value); }
        }
        /// <summary>
        /// 顯示資料的datagrid
        /// </summary>
        public static readonly DependencyProperty DataObjectProperty =
            DependencyProperty.Register("DataObject", typeof(System.Windows.Controls.DataGrid), typeof(Student_Action),null );
        public DataGrid DataObject
        {
            get { return (DataGrid)GetValue(Student_Action.DataObjectProperty); }
            set { SetValue(Student_Action.DataObjectProperty, value); }
        }
        #endregion

          又寫了一個固定的資料來源,(students的定義未寫,就是幾個屬性)

          List<Students> DTO = new List<Students>();
        public Student_Action()
  {
   // 在此點下面插入建立對象所需的代碼。
            for (int a = 1; a < 10; a++)
            {
                Students stu = new Students();
                stu.Id = a;
                stu.Name = "姓名" + a.ToString();
                stu.ClassName = "班級" + a.ToString();
                DTO.Add(stu);
            }
  }

      要想載入資料,需要重寫OnAttached方法,在這裡邊定義事件等..:(感覺載入資料寫在按鈕的載入事件裡不好,應該寫在datagrid的載入事件裡,如又興趣可以自己試一下)

      protected override void OnAttached()
        {
            base.OnAttached();
            (this.AssociatedObject as Button).Loaded += new RoutedEventHandler(Student_Action_Loaded);
            (this.AssociatedObject as Button).Click += new RoutedEventHandler(Student_Action_Click);
        }

 

 void Student_Action_Loaded(object sender, RoutedEventArgs e)
        {
            (this.DataObject as DataGrid).ItemsSource = DTO;
        }

寫到這裡運行項目,變會載入出資料來。

好了,先寫到這把,下一篇會把添加資料的寫出來,

 

哎呀,My God  ,第一次寫部落格,還真不知道該怎麼寫,請大家原諒小弟寫的粗糙,沒有頭緒,歡迎大哥大嫂,指點批評,好了!!!

聯繫我們

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