关于WPF的布局部分不想写了,感觉比较散乱,其实找本书看看基本也讲的八九不离十了,一般都讲的很清楚,又好懂
无非就是Grid,StackPanel,DockPanel,WrapPanel,Canvas那些,用的熟了自然也没什么障碍的.
唯一一个大多数书上写的不清不楚的就是"依赖属性"这个玩意,
MSDN是这样解释依赖属性的
当您定义自己的属性并需要它们支持 Windows Presentation Foundation (WPF) 功能的诸多方面(包括样式、数据绑定、继承、动画和默认值)时,应将其实现为依赖项属性。
那么根据我的理解,依赖属性就是普通属性之上,再附加一些WPF特有的功能,就变成WPF里面适用的属性了,这样的属性就叫依赖属性罗??最近在看<<WPF编程宝典-使用C#2012和.NET4.5(第四版)>>这本书,看的是似懂非懂
书中几条主要脉络讲"依赖属性".
1.理解依赖项属性
书中讲的就一句,"出于性能方面的考虑",接下来的话就有点不通顺了."如果不承受这一额外负担,普通属性就不能支持这些依赖项属性的所有功能",拜托!这句太不和谐了吧,看起来都不像中文啊,很绕不说,简直不知道它想说什么!再说,这也只是从性能方面来说,还有别的优点吗?
2.定义依赖项属性
书中提到,定义依赖项属性应该始终保持可用,什么意思?又说到,甚至可能需要在多个类之间共享信息.必须将DependencyProperty对象定义 为与其相关联的类的静态字段.说了半天,总算说到点子上,怪了,谈第一点时不说为什么出于性能的考虑,语焉不详,第二点谈定义依赖项属性的时候,反而提了,原来就是要缓存这些相同的值,这些值在内存里只保有一份就可以了,不必拷贝多份,从而节约内存的使用.
下面在网上找到一个比较能说明问题的例子,比如说Buttton这个对象,它必然继承自DependencyObject(所有控件的基类),但DependencyObject不是它的直接父类,它的直接父类是ButtonBase,它的整个继承链是这样的:
Button->ButtonBase->ContentControl->Control->FrameworkElement->UIElement->Visual->DependencyObject->…
如此一来,Button从父类继承的属性可想而知有多少了! 对Button来说,大多数属性并没有被修改,仍然保持着父类定义时的默认值。通常情况,在整个Button对象的生命周期里,也只有少部分属性被修改,大多数属性一直保持着初始值。每个字段,都需要占用4K等不等的内存,这里,就出现了期望可以优化的地方!
我并没有亲自用Reflector去读DependencyObject的源码,大致看了一下网上和书上对源码的解释和引用,只知道里面定义了一个字典对象用来缓存在DependencyObject里注册的依赖属性项,这足以解释了之前的说法.
3.注册依赖属性
这书里描述的是更加的不清晰,一上来就是一段代码
- static FrameworkElement()
- {
- FramworkPropertyMetadata metadata =
- new FrameworkPropertyMetadata(new Thickness(),FrameworkPropertyMetadataOptions.AffectsMeasure);
- MarginProperty = DependencyProperty.Register("Margin",typeof(Thickness),typeof(FrameworkElement),metadata,
- new ValidateValueCallback(FrameworkElement.IsMarginValid));
- ...
- }
FramworkPropertyMetadata这个东西书上说," 指示希望通过依赖项属性使用什么服务(如支持数据绑定,动画以及日志)",什么意思?不明白的很!从名字上看,它是"Metadata",是元数据,
看MSDN对它的解释
报告或应用依赖项属性的元数据,具体来说,就是添加特定于框架的属性系统特征。
FramworkPropertyMetadata 又继承自 PropertyMetadata
又找到MSDN对它的解释
定义依赖项对象在应用于特定类型(包括该属性向其注册的条件)时行为的某些方面。
看了这些,真的是一头雾水,马上又找到MSDN上面一个完整例子下载来看
分析一下这个程序中,注册并使用依赖属性的整个过程
a)首先定义了一个枚举类型
- public enum ShirtColors
- {
- None,
- White,
- Red,
- Green,
- Yellow
- }
- public static readonly DependencyProperty ShirtColorProperty = DependencyProperty.Register("ShirtColor", typeof(ShirtColors), typeof(Shirt), new FrameworkPropertyMetadata(ShirtColors.None));
c)然后定义一个属性包装器对它进行包装
- public ShirtColors ShirtColor
- {
- get { return (ShirtColors)GetValue(ShirtColorProperty); }
- set { SetValue(ShirtColorProperty, value); }
- }
MSDN对GetValue这么解释
返回 DependencyObject 的此实例上的依赖项属性的当前有效值。
跟普通属性的区别可能就体现在"有效值"这三个字上!
d)定义依赖属性的时候,还可以定义事件,下面两个一个是改变属性时触发的,一个是验证时触发的
- public static readonly DependencyProperty ShirtTypeProperty = DependencyProperty.Register(
- "ShirtType",
- typeof(ShirtTypes),
- typeof(Shirt),
- new FrameworkPropertyMetadata(
- ShirtTypes.None,
- new PropertyChangedCallback(OnShirtTypeChangedCallback)
- ), new ValidateValueCallback(ShirtValidateCallback));
这个程序的逻辑大体是这样
如上图,在点击"Bowling"和"Dress"时,下面的Button Color 才会出现,点其它选项就隐藏起来,
这个例子的全部代码如下
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Media;
- using System.Windows.Shapes;
- namespace SDKSample
- {
- public partial class DPCustom
- {
- private Shirt myShirt;
- public void OnInit(object sender, EventArgs e)
- {
- myShirt = new Shirt();
- PopulateListbox(ShirtChoice, Enum.GetValues(typeof(ShirtTypes)));
- PopulateListbox(ShirtColorChoice, Enum.GetValues(typeof(ShirtColors)));
- PopulateListbox(ButtonChoice, Enum.GetValues(typeof(ButtonColors)));
- myShirt.ButtonColorChanged += new RoutedEventHandler(UIButtonColorChanged);
- }
- private void PopulateListbox (FrameworkElement fe, Array values) {
- ListBox lb = fe as ListBox;
- for (int j = 1; j < values.Length; j++)
- {
- ListBoxItem lbi = new ListBoxItem();
- lbi.Content = (Enum)values.GetValue(j);
- lb.Items.Add(lbi);
- }
- }
- private void ChooseShirt(object sender, SelectionChangedEventArgs e)
- {
- ListBoxItem lbi = ((e.Source as ListBox).SelectedItem as ListBoxItem);
- myShirt.ShirtType = (ShirtTypes)lbi.Content;
- }
- private void ChooseShirtColor(object sender, SelectionChangedEventArgs e)
- {
- ListBoxItem lbi = ((e.Source as ListBox).SelectedItem as ListBoxItem);
- myShirt.ShirtColor = (ShirtColors)lbi.Content;
- }
- private void ChooseButtonColor(object sender, SelectionChangedEventArgs e)
- {
- ListBoxItem lbi = ((e.Source as ListBox).SelectedItem as ListBoxItem);
- myShirt.ButtonColor = (ButtonColors)lbi.Content;
- }
- private void UIButtonColorChanged(object sender, RoutedEventArgs e)
- {
- Shirt s = (Shirt)e.Source;
- ButtonColors b = s.ButtonColor;
- if (b == ButtonColors.None)
- {
- ButtonChoice.Visibility = Visibility.Hidden;
- ButtonChoiceLabel.Visibility = Visibility.Hidden;
- }
- else
- {
- ButtonChoice.Visibility = Visibility.Visible;
- ButtonChoiceLabel.Visibility = Visibility.Visible;
- }
- }
- }
- public enum ShirtTypes {
- None,
- Tee,
- Bowling,
- Dress,
- Rugby
- }
- public enum ShirtColors
- {
- None,
- White,
- Red,
- Green,
- Yellow
- }
- public enum ButtonColors
- {
- None,
- Black,
- White,
- Brown,
- Gray
- }
- public class Shirt : Canvas {
- public static readonly DependencyProperty ShirtColorProperty = DependencyProperty.Register("ShirtColor", typeof(ShirtColors), typeof(Shirt), new FrameworkPropertyMetadata(ShirtColors.None));
- public ShirtColors ShirtColor
- {
- get { return (ShirtColors)GetValue(ShirtColorProperty); }
- set { SetValue(ShirtColorProperty, value); }
- }
- public static readonly DependencyProperty ShirtTypeProperty = DependencyProperty.Register(
- "ShirtType",
- typeof(ShirtTypes),
- typeof(Shirt),
- new FrameworkPropertyMetadata(
- ShirtTypes.None,
- new PropertyChangedCallback(OnShirtTypeChangedCallback)
- ), new ValidateValueCallback(ShirtValidateCallback));
- public ShirtTypes ShirtType {
- get {return(ShirtTypes)GetValue(ShirtTypeProperty);}
- set {SetValue(ShirtTypeProperty,value);}
- }
- //<SnippetValidateValueCallback>
- private static bool ShirtValidateCallback(object value)
- {
- ShirtTypes sh = (ShirtTypes) value;
- return (sh==ShirtTypes.None || sh == ShirtTypes.Bowling || sh == ShirtTypes.Dress || sh == ShirtTypes.Rugby || sh == ShirtTypes.Tee);
- }
- //</SnippetValidateValueCallback>
- private static void OnShirtTypeChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- d.CoerceValue(ButtonColorProperty);
- }
- //<SnippetPropertyChangedCallbackPlusDPDef>
- public static readonly DependencyProperty ButtonColorProperty = DependencyProperty.Register(
- "ButtonColor",
- typeof(ButtonColors),
- typeof(Shirt),
- new FrameworkPropertyMetadata(
- ButtonColors.Black,
- new PropertyChangedCallback(OnButtonColorChangedCallback),
- new CoerceValueCallback(CoerceButtonColor))
- );
- public ButtonColors ButtonColor
- {
- get { return (ButtonColors)GetValue(ButtonColorProperty); }
- set { SetValue(ButtonColorProperty, value); }
- }
- private static void OnButtonColorChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- RoutedEventArgs newargs = new RoutedEventArgs(ButtonColorChangedEvent);
- (d as FrameworkElement).RaiseEvent(newargs);
- }
- //</SnippetPropertyChangedCallbackPlusDPDef>
- //<SnippetCoerceValueCallback>
- private static object CoerceButtonColor(DependencyObject d, object value)
- {
- ShirtTypes newShirtType = (d as Shirt).ShirtType;
- if (newShirtType == ShirtTypes.Dress || newShirtType == ShirtTypes.Bowling)
- {
- return ButtonColors.Black;
- }
- return ButtonColors.None;
- }
- //</SnippetCoerceValueCallback>
- //<SnippetEventManagerClass>
- public static readonly RoutedEvent ButtonColorChangedEvent = EventManager.RegisterRoutedEvent("ButtonColorChanged",RoutingStrategy.Bubble,typeof(DependencyPropertyChangedEventHandler),typeof(Shirt));
- public event RoutedEventHandler ButtonColorChanged {
- add {AddHandler(ButtonColorChangedEvent,value);}
- remove { RemoveHandler(ButtonColorChangedEvent, value); }
- }
- //</SnippetEventManagerClass>
- }
- }
上面一共用了三种委托
CoerceValueCallback (为只要重新计算依赖项属性值或专门请求强制转换时就调用的方法提供一个模板)
PropertyChangedCallback (表示在依赖项对象的有效属性值更改时调用的回调。)
ValidateValueCallback (表示用作回调的方法,用于验证依赖项属性的值是否有效。)
这个例子可能举的不算太好,MSDN就是这样.
下面有个博客园的例子,感觉比较符合实战