WPF一个类似于QQ面板的GroupShelf控件.docx
- 文档编号:3782855
- 上传时间:2023-05-06
- 格式:DOCX
- 页数:22
- 大小:71.71KB
WPF一个类似于QQ面板的GroupShelf控件.docx
《WPF一个类似于QQ面板的GroupShelf控件.docx》由会员分享,可在线阅读,更多相关《WPF一个类似于QQ面板的GroupShelf控件.docx(22页珍藏版)》请在冰点文库上搜索。
WPF一个类似于QQ面板的GroupShelf控件
从整体来看,这个控件应该同ListBox,ListView这类控件一样,是一个ItemsControl,而中间的项,就是它的Item。
因此,为了完成一个这样的控件,至少需要两个东西:
GroupShelf:
也就是充当容器角色的控件
GroupShelfItem:
即这个控件中的项
qq_face_1.png(24.78K)
2008-8-2212:
36:
34
qq_face.png(25.49K)
2008-8-2212:
36:
34
其中,GroupShelf需要保证某项的展开同时,其他项被折叠。
而GroupShelfItem需要提供Header和Content,同时,需要能支持展开的空能。
【控件的实现】
GroupShelfItem
首先,我们从GroupShelfItem入手,因为它比较单纯,在HeaderedContentControl的基础上提供展开,收缩功能即可:
1. ///
2. ///GroupShelfItem
3. ///
4. publicclassGroupShelfItem:
HeaderedContentControl
5. {
6. #regionIsExpanded
7.
8. publicboolIsExpanded
9. {
10. get{return(bool)GetValue(IsExpandedProperty);}
11. set{SetValue(IsExpandedProperty,value);}
12. }
13.
14. //UsingaDependencyPropertyasthebackingstoreforIsSelected. Thisenablesanimation,styling,binding,etc
15. publicstaticreadonlyDependencyPropertyIsExpandedProperty=DependencyProperty.Register(
16. "IsExpanded",typeof(bool),typeof(GroupShelfItem),newPropertyMetadata(false,newPropertyChangedCallback(OnIsExpandedChanged)));
17.
18. privatestaticvoidOnIsExpandedChanged(DependencyObjectsender,DependencyPropertyChangedEventArgse)
19. {
20. GroupShelfItemitem=senderasGroupShelfItem;
21. if(item!
=null)
22. {
23. item.OnIsExpandedChanged(e);
24. }
25. }
26.
27. protectedvirtualvoidOnIsExpandedChanged(DependencyPropertyChangedEventArgse)
28. {
29. boolnewValue=(bool)e.NewValue;
30.
31. if(newValue)
32. {
33. this.OnExpanded();
34. }
35. else
36. {
37. this.OnCollapsed();
38. }
39. }
40.
41. #endregion
42.
43. #regionSelectionEvents
44.
45. ///
46. ///Raisedwhenselected
47. ///
48. publiceventRoutedEventHandlerExpanded
49. {
50. add{AddHandler(ExpandedEvent,value);}
51. remove{RemoveHandler(ExpandedEvent,value);}
52. }
53.
54. publicstaticRoutedEventExpandedEvent=EventManager.RegisterRoutedEvent(
55. "Expanded",RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(GroupShelfItem));
56.
57. ///
58. ///Raisedwhenunselected
59. ///
60. publiceventRoutedEventHandlerCollapsed
61. {
62. add{AddHandler(CollapsedEvent,value);}
63. remove{RemoveHandler(CollapsedEvent,value);}
64. }
65.
66. publicstaticRoutedEventCollapsedEvent=EventManager.RegisterRoutedEvent(
67. "Collapsed",RoutingStrategy.Bubble,typeof(RoutedEventHandler),typeof(GroupShelfItem));
68.
69. protectedvirtualvoidOnExpanded()
70. {
71. GroupShelfparentGroupShelf=this.ParentGroupShelf;
72. if(parentGroupShelf!
=null)
73. {
74. parentGroupShelf.ExpandedItem=this;
75. }
76. RaiseEvent(newRoutedEventArgs(ExpandedEvent,this));
77. }
78.
79. protectedvirtualvoidOnCollapsed()
80. {
81. RaiseEvent(newRoutedEventArgs(CollapsedEvent,this));
82. }
83.
84. #endregion
85.
86. #regionExpandCommand
87.
88. publicstaticRoutedCommandExpandCommand=newRoutedCommand("Expand",typeof(GroupShelfItem));
89.
90. privatestaticvoidOnExecuteExpand(objectsender,ExecutedRoutedEventArgse)
91. {
92. GroupShelfItemitem=senderasGroupShelfItem;
93. if(!
item.IsExpanded)
94. {
95. item.IsExpanded=true;
96. }
97. }
98.
99. privatestaticvoidCanExecuteExpand(objectsender,CanExecuteRoutedEventArgse)
100. {
101. e.CanExecute=senderisGroupShelfItem;
102. }
103.
104. #endregion
105.
106. publicGroupShelfParentGroupShelf
107. {
108. get{returnItemsControl.ItemsControlFromItemContainer(this)asGroupShelf;}
109. }
110.
111. staticGroupShelfItem()
112. {
113. DefaultStyleKeyProperty.OverrideMetadata(typeof(GroupShelfItem),newFrameworkPropertyMetadata(typeof(GroupShelfItem)));
114.
115. CommandBindingexpandCommandBinding=newCommandBinding(ExpandCommand,OnExecuteExpand,CanExecuteExpand);
116. CommandManager.RegisterClassCommandBinding(typeof(GroupShelfItem),expandCommandBinding);
117. }
118. }
默认的Style
1. 2. xmlns=" 3. xmlns: x=" 4. xmlns: local="clr-namespace: GroupShelfDemo.Controls"> 5. Typelocal: GroupShelfItem}"> 6. 7. 8. Typelocal: GroupShelfItem}"> 9. 10. BorderBrush="{TemplateBindingBorderBrush}" 11. BorderThickness="{TemplateBindingBorderThickness}"> 12. 13. 14. C 15. C 16. ContentTemplateSelector="{TemplateBindingHeaderTemplateSelector}" 17. C 18. Command="{BindingSource={x: Staticlocal: GroupShelfItem.ExpandCommand}}"/> 19. Name="ContentHost"DockPanel.Dock="Bottom" 20. C 21. C 22. ContentTemplateSelector="{TemplateBindingContentTemplateSelector}" 23. C> 24. 25. Name="ContentHostHeightTransform"ScaleY="0.0"/> 26. 27. 28. 29. 30. 31. Key="OnExpanded"> 32. 00: 00" 33. Storyboard.TargetName="ContentHostHeightTransform" 34. Storyboard.TargetProperty="ScaleY"> 35. 00: 00.08"Value="1"/> 36. 37. 38. Key="OnCollapsed"> 39. 00: 00" 40. Storyboard.TargetName="ContentHostHeightTransform" 41. Storyboard.TargetProperty="ScaleY"> 42. 00: 00.08"Value="0"/> 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60.
GroupShelfItem提供了一个Command来操作它的展开和收缩。
同时,在Expand的时候通知GroupShelf处理。
在默认的控件模板中通过按钮来触发这个Command。
GroupShelfPanel
GroupShelf的主要工作就是维护GroupShelfItem展开和收缩时的状态处理。
但是,按照WPF的方式,这个布局的工作不应该由它来完成,而是由我们提供一个ItemsPanel给它。
所以,在GroupShelf之前,GroupShelfPanel应运而生。
写一个Panel最重要的工作就是重载MeasureOverride和ArrangeOverride两个方法。
分析GroupShelfPanel的行为,其实是“指定的孩子填充剩余空间”。
就系统提供的Panel来说,DockPanel跟它的行为是最接近的,因为DockPanel提供了LastChildFill的行为。
既然如此,我们就打开Reflector,从DockPanel里面“借”点代码过来用用:
1. publicclassGroupShelfPanel:
Panel
2. {
3. ///
4. ///要填充的孩子
5. ///
6. publicUIElementChildToFill
7. {
8. get{return(UIElement)GetValue(ChildToFillProperty);}
9. set{SetValue(ChildToFillProperty,value);}
10. }
11.
12. publicstaticreadonlyDependencyPropertyChildToFillProperty=DependencyProperty.Register(
13. "ChildToFill",typeof(UIElement),typeof(GroupShelfPan
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- WPF 一个 类似于 QQ 面板 GroupShelf 控件