在開發應用中WPF優點的體現
WPF為什么會被開發人員如此重視,在開發過程中又為何會占據著重要的地位呢?這就要先從WPF優點來分析。我們將會為大家總結出幾個WPF優點方便大家理解。#t#
WPF優點之讓控件更靈活的Data Template
C#代碼
- GridViewColumn gvcName =
new GridViewColumn(); - gvcName.Header = "Name";
- DataTemplate nameTemplate =
new DataTemplate(); - FrameworkElementFactory
nameFactory = new Framework
ElementFactory(typeof(Contact
PropertyLabel)); - Binding binding = new
Binding("Name"); - binding.Mode = BindingMode.TwoWay;
- nameFactory.SetBinding
(ContentProperty, binding); - nameTemplate.VisualTree =
nameFactory; - gvcName.CellTemplate =
nameTemplate; - gvContactList.Columns.Add(gvcName);
- GridViewColumn gvcName =
new GridViewColumn(); - gvcName.Header = "Name";
- DataTemplate nameTemplate =
new DataTemplate(); - FrameworkElementFactory nameFactory =
new FrameworkElementFactory
(typeof(ContactPropertyLabel)); - Binding binding = new Binding("Name");
- binding.Mode = BindingMode.TwoWay;
- nameFactory.SetBinding
(ContentProperty, binding); - nameTemplate.VisualTree = nameFactory;
- gvcName.CellTemplate = nameTemplate;
- gvContactList.Columns.Add(gvcName);
這段代碼可以給一個ListView的一列做雙向綁定,并且指定這一行的渲染控件。
WPF優點之比Windows Forms更強的Binding
C#代碼
- public class Contact :
DependencyObject- {
- public static DependencyProperty
NameProperty = DependencyProperty.
Register("Name", typeof (string),
typeof (Contact));- public string Name
- {
- get { return (string) GetValue
(NameProperty); }- set { SetValue(NameProperty, value); }
- }
- }
- public class Contact : DependencyObject
- {
- public static DependencyProperty
NameProperty = DependencyProperty.
Register("Name", typeof (string),
typeof (Contact));- public string Name
- {
- get { return (string) GetValue
(NameProperty); }- set { SetValue(NameProperty,
value); }- }
- }
DependencyObject + DependencyProperty使得屬性設置可以自動觸發ValueChanged事件,從而讓Binding進行更新。
WPF優點之讓排版更靈活的各種Layout控件
對于普通界面的排版,用Grid+Border
對于要動態添加刪除的界面排版,在需要動態增刪的位置使用StackPanel