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