在開發(fā)應(yīng)用中WPF優(yōu)點(diǎn)的體現(xiàn)
WPF為什么會被開發(fā)人員如此重視,在開發(fā)過程中又為何會占據(jù)著重要的地位呢?這就要先從WPF優(yōu)點(diǎn)來分析。我們將會為大家總結(jié)出幾個WPF優(yōu)點(diǎn)方便大家理解。#t#
WPF優(yōu)點(diǎn)之讓控件更靈活的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優(yōu)點(diǎn)之比Windows Forms更強(qiáng)的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使得屬性設(shè)置可以自動觸發(fā)ValueChanged事件,從而讓Binding進(jìn)行更新。
WPF優(yōu)點(diǎn)之讓排版更靈活的各種Layout控件
對于普通界面的排版,用Grid+Border
對于要動態(tài)添加刪除的界面排版,在需要動態(tài)增刪的位置使用StackPanel