在开发应用中WPF优点的体现

开发 开发工具
WPF优点包括:拥有让控件更灵活的Data Template ;具有比Windows Forms更强的Binding ;可以让排版更灵活的各种Layout控件等。

WPF为什么会被开发人员如此重视,在开发过程中又为何会占据着重要的地位呢?这就要先从WPF优点来分析。我们将会为大家总结出几个WPF优点方便大家理解。#t#

WPF优点之让控件更灵活的Data Template

C#代码

  1. GridViewColumn gvcName = 
    new GridViewColumn();   
  2. gvcName.Header = "Name";   
  3. DataTemplate nameTemplate = 
    new DataTemplate();   
  4. FrameworkElementFactory 
    nameFactory = new Framework
    ElementFactory(typeof(Contact
    PropertyLabel));   
  5. Binding binding = new 
    Binding("Name");   
  6. binding.Mode = BindingMode.TwoWay;   
  7. nameFactory.SetBinding
    (ContentProperty, binding);   
  8. nameTemplate.VisualTree = 
    nameFactory;   
  9. gvcName.CellTemplate = 
    nameTemplate;   
  10. gvContactList.Columns.Add(gvcName);   
  11. GridViewColumn gvcName = 
    new GridViewColumn();  
  12. gvcName.Header = "Name";  
  13. DataTemplate nameTemplate = 
    new DataTemplate();  
  14. FrameworkElementFactory nameFactory = 
    new FrameworkElementFactory
    (typeof(ContactPropertyLabel));  
  15. Binding binding = new Binding("Name");  
  16. binding.Mode = BindingMode.TwoWay;  
  17. nameFactory.SetBinding
    (ContentProperty, binding);  
  18. nameTemplate.VisualTree = nameFactory;  
  19. gvcName.CellTemplate = nameTemplate;  
  20. gvContactList.Columns.Add(gvcName); 

 

这段代码可以给一个ListView的一列做双向绑定,并且指定这一行的渲染控件。

WPF优点之比Windows Forms更强的Binding

C#代码

  1. public class Contact : 
    DependencyObject   
  2. {   
  3. public static DependencyProperty 
    NameProperty = DependencyProperty.
    Register("Name", typeof (string), 
    typeof (Contact));   
  4. public string Name   
  5. {   
  6. get { return (string) GetValue
    (NameProperty); }   
  7. set { SetValue(NameProperty, value); }   
  8. }   
  9. }   
  10. public class Contact : DependencyObject  
  11. {  
  12. public static DependencyProperty 
    NameProperty = DependencyProperty.
    Register("Name", typeof (string), 
    typeof (Contact));  
  13. public string Name  
  14. {  
  15. get { return (string) GetValue
    (NameProperty); }  
  16. set { SetValue(NameProperty, 
    value); }  
  17. }  

 

DependencyObject + DependencyProperty使得属性设置可以自动触发ValueChanged事件,从而让Binding进行更新。

WPF优点之让排版更灵活的各种Layout控件

对于普通界面的排版,用Grid+Border

对于要动态添加删除的界面排版,在需要动态增删的位置使用StackPanel

责任编辑:曹凯 来源: javaeye.com
相关推荐

2009-12-29 14:00:02

WPF Dispatc

2010-03-25 11:08:08

Python优点

2009-12-24 17:52:05

WPF触发器

2009-12-25 15:29:12

WPF缺陷

2009-12-25 16:40:49

WPF优势

2010-03-03 17:52:31

Python线程同步

2010-01-08 18:02:33

VB.NET事件

2010-01-14 10:35:34

VB.NET指针

2010-02-25 17:22:39

WCF服务行为

2010-03-04 10:35:51

Python下划线

2010-02-02 13:15:00

C++ lambda函

2010-03-19 17:15:43

2009-12-28 16:45:31

WPF窗体

2010-01-15 13:30:53

VB.NET Tool

2010-03-24 14:55:19

Python编程语言

2009-12-28 09:50:08

WPF数据绑定

2018-10-11 10:38:31

前端JavaScript编程语言

2012-08-24 09:58:09

ReactorDSSC

2010-03-29 11:17:33

Oracle Spat

2009-12-07 13:20:14

PHP技术应用
点赞
收藏

51CTO技术栈公众号