WPF : 如何選用Template, UserControl, CustomControl

來源:互聯網
上載者:User

使用順序: DataTemplate, UserControl, ControlTemplate, CustomControl

  1. 盡量使用Template. Template分2種: "純XAML"和"XAML+CodeBehind"
    盡量使用"純XAML"類型的Template, 純XAML的Template可以放在任何資源中, 包括沒有CodeBehind的ResouceDictionary中.
    方法:
    - 用Command來操作Model
    - 用DataTrigger 響應Model的事件, 設定UI屬性
    - 用Trigger 響應控制項事件, 設定控制項屬性
    - 用Binding, Converter實現Model和UI之間的資料轉換
  2. DataTemplate用於描述UI控制項和模型資料的對應關係, 使用DataTemplate的關鍵是:1)選擇哪種控制項 2)控制項和模型間的Binding 3)DataTrigger處理模型事件
  3. ControlTemplate用於重新定義UI控制項, 少用. 主要用於開發可重用性控制項.
  4. UserControl用於描述UI控制項的Style以及UI控制項之間的合作. 滑鼠事件之類應該由UserControl來處理. UserControl只處理UI控制項, 不處理模型資料.
    UserControl側重於控制項的組合.
  5. 使用UserControl和DataTemplate之間的區別 :
    如果多個UI控制項的互動非常複雜, 則封裝在UserControl中. 當UI和Model之間界限比較清楚時, 用DataTemplate.
    UserControl用於視覺表現和使用者互動, DataTemplate用於資料和控制項之間綁定.
  6. 需要讓使用者自訂ControlTemplate時, 使用CustomControl.

聯繫我們

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