WPF一个类似于QQ面板的GroupShelf控件Word文档下载推荐.docx
《WPF一个类似于QQ面板的GroupShelf控件Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《WPF一个类似于QQ面板的GroupShelf控件Word文档下载推荐.docx(22页珍藏版)》请在冰点文库上搜索。
![WPF一个类似于QQ面板的GroupShelf控件Word文档下载推荐.docx](https://file1.bingdoc.com/fileroot1/2023-5/6/de49f13a-290c-4c47-933a-e407db384bdd/de49f13a-290c-4c47-933a-e407db384bdd1.gif)
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"
68.
69.
protectedvirtualvoidOnExpanded()
70.
71.
GroupShelfparentGroupShelf=this.ParentGroupShelf;
72.
if(parentGroupShelf!
73.
74.
parentGroupShelf.ExpandedItem=this;
75.
76.
RaiseEvent(newRoutedEventArgs(ExpandedEvent,this));
77.
78.
79.
protectedvirtualvoidOnCollapsed()
80.
81.
RaiseEvent(newRoutedEventArgs(CollapsedEvent,this));
82.
83.
84.
85.
86.
#regionExpandCommand
87.
88.
publicstaticRoutedCommandExpandCommand=newRoutedCommand("
Expand"
typeof(GroupShelfItem));
89.
90.
privatestaticvoidOnExecuteExpand(objectsender,ExecutedRoutedEventArgse)
91.
92.
93.
if(!
item.IsExpanded)
94.
95.
item.IsExpanded=true;
96.
97.
98.
99.
privatestaticvoidCanExecuteExpand(objectsender,CanExecuteRoutedEventArgse)
100.
101.
e.CanExecute=senderisGroupShelfItem;
102.
103.
104.
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.<
ResourceDictionary
xmlns="
xmlns:
x="
local="
clr-namespace:
GroupShelfDemo.Controls"
>
<
StyleTargetType="
{x:
Typelocal:
GroupShelfItem}"
SetterProperty="
Template"
7.
Setter.Value>
ControlTemplateTargetType="
BorderBackground="
{TemplateBindingBackground}"
BorderBrush="
{TemplateBindingBorderBrush}"
BorderThickness="
{TemplateBindingBorderThickness}"
DockPanel>
13.
ButtonDockPanel.Dock="
Top"
C
ContentTemplateSelector="
{TemplateBindingHeaderTemplateSelector}"
17.
Command="
{BindingSource={x:
Staticlocal:
GroupShelfItem.ExpandCommand}}"
/>
ContentPresenterx:
Name="
ContentHost"
DockPanel.Dock="
Bottom"
{TemplateBindingContentTemplateSelector}"
C>
ContentPresenter.LayoutTransform>
ScaleTransformx:
ContentHostHeightTransform"
ScaleY="
0.0"
26.
/ContentPresenter.LayoutTransform>
/ContentPresenter>
/DockPanel>
/Border>
30.
ControlTemplate.Resources>
Storyboardx:
Key="
OnExpanded"
DoubleAnimationUsingKeyFramesBeginTime="
00:
00"
Storyboard.TargetName="
Storyboard.TargetProperty="
ScaleY"
SplineDoubleKeyFrameKeyTime="
00.08"
Value="
1"
/DoubleAnimationUsingKeyFrames>
/Storyboard>
OnCollapsed"
40.
42.
0"
44.
/ControlTemplate.Resources>
ControlTemplate.Triggers>
TriggerProperty="
True"
Trigger.EnterActions>
BeginStoryboardStoryboard="
{StaticResourceOnExpanded}"
/Trigger.EnterActions>
Trigger.ExitActions>
{StaticResourceOnCollapsed}"
53.
/Trigger.ExitActions>
/Trigger>
/ControlTemplate.Triggers>
56.
/ControlTemplate>
/Setter.Value>
/Setter>
/Style>
60.<
/ResourceDictionary>
GroupShelfItem提供了一个Command来操作它的展开和收缩。
同时,在Expand的时候通知GroupShelf处理。
在默认的控件模板中通过按钮来触发这个Command。
GroupShelfPanel
GroupShelf的主要工作就是维护GroupShelfItem展开和收缩时的状态处理。
但是,按照WPF的方式,这个布局的工作不应该由它来完成,而是由我们提供一个ItemsPanel给它。
所以,在GroupShelf之前,GroupShelfPanel应运而生。
写一个Panel最重要的工作就是重载MeasureOverride和ArrangeOverride两个方法。
分析GroupShelfPanel的行为,其实是“指定的孩子填充剩余空间”。
就系统提供的Panel来说,DockPanel跟它的行为是最接近的,因为DockPanel提供了LastChildFill的行为。
既然如此,我们就打开Reflector,从DockPanel里面“借”点代码过来用用:
publicclassGroupShelfPanel:
Panel
///要填充的孩子
publicUIElementChildToFill
get{return(UIElement)GetValue(ChildToFillProperty);
set{SetValue(ChildToFillProperty,value);
11.
publicstaticreadonlyDependencyPropertyChildToFillProperty=DependencyProperty.Register(
ChildToFill"
typeof(UIElement),typeof(GroupShelfPan