silverlight
发布时间:2022-05-03 发布网站:大佬教程 code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverlight入门系列]右键菜单ContextMenu工具栏Toolbar和SplitButton,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
概述
Silverlight4 Toolkit提供了Co
ntextMenu和MenuItem控件来实现右键菜单,很方
便。另外, Silverlight4 Toolkit提供了 PopUp控件来实现弹出式内容,里面可以放任何子控件,利用 IsOpen属性来控制显示隐藏。Silverlight工具栏Toolbar制作也很简单,就是用一个StackPanel横向摆一些button即可,button的内容
Silverlight4 Toolkit提供了Co
ntextMenu和MenuItem控件来实现右键
菜单,很方
便。另外,
Silverlight4 Toolkit提供了
PopUp控件来实现弹出式
内容,里面可以放任何子控件,利用
IsOpen属性来控制
显示隐藏。Silverlight工具栏Toolbar制作也很简单,就是用
一个StackPanel横向摆一些button即可,button的
内容就是
图片,或者
图片加
文字均可。
上下文菜单ContextMenu的Silverlight Demo源码下载请点击
这儿,
SplitButton的Silverlight Demo源码下载请点击
这儿,Silverlight工具栏Toolbar的源码
在本文末尾。另外微软的一款Silverlight Split Button的源码在
这儿下载。各种实现
效果如下几幅图:
Silverlight SplitButton
Silverlight ContextMenu
Silverlight工具栏 Toolbar
<
splitButton:SplitButton
Content
="Open"
Click
="Open_Click"
>
splitButton:SplitButton.buttonMenuItemssource
toolkit:MenuItem
Header
toolkit:MenuItem.Icon
Image
source
="/SilverlightApplication3;component/images/search.png"
Width
="14"
Height
="14"
/>
</
toolkit:MenuItem
="Open read-only"
="OpenReadOnly_Click"
="Open as copy"
="OpenCopy_Click"
splitButton:SplitButton
>
Silverlight SplitButton源码
1
using
System;
2
System.Collections.ObjectModel;
3
System.Windows;
4
System.Windows.Controls;
5
System.Windows.Input;
6
7
namespace
Delay
8
{
9
///
<sumMary>
10
Implements a "split button" for Silverlight and WPF.
11
</sumMary>
12
[TemplatePart(Name
=
SplitElementName, Type
typeof
(UIElement))]
13
public
class
SplitButton : Button
14
{
15
16
Stores the public NAME of the split element.
17
18
private
const
String
SplitElementName
"
SplitElement
"
;
19
20
21
Stores a reference to the split element.
22
23
UIElement _splitElement;
24
25
26
Stores a reference to the ContextMenu.
27
28
ContextMenu _contextMenu;
29
30
31
32
Stores the initial LOCATIOn of the ContextMenu.
33
34
Point _contextMenuInitialOffset;
35
36
37
Stores the backing collection for the ButtonMenuItemssource property.
38
39
ObservableCollection
<
object
>
_buttonMenuItemssource
new
();
40
41
42
Gets the collection of items for the split button's menu.
43
44
Collection
ButtonMenuItemssource {
get
{
return
_buttonMenuItemssource; } }
45
46
47
Gets or sets a value inDicaTing whetherthe mouse is over the split element.
48
49
protected
bool
IsMouSEOverSplitElement {
;
set
; }
50
51
52
Initializes a new instance of the SplitButton class.
53
54
SplitButton()
55
{
56
DefaultStyleKey
(SplitButton);
57
}
58
59
60
Called when the template is changed.
61
62
override
void
OnApplyTemplate()
63
64
//
Unhook exisTing handlers
65
if
(
null
!=
_splitElement)
66
{
67
_splitElement.MouseEnter
-=
MouseEventHandler(SplitElement_MouseEnter);
68
_splitElement.MouSELEave
MouseEventHandler(SplitElement_MouSELEavE);
69
_splitElement
70
}
71
_contextMenu)
72
73
_contextMenu.opened
routedEventHandler(ContextMenu_Opened);
74
_contextMenu.Closed
routedEventHandler(ContextMenu_Closed);
75
_contextMenu
76
77
78
Apply new template
79
base
.onApplyTemplate();
80
81
Hook new event handlers
82
_splitElement
GetTemplateChild(SplitElementName)
as
UIElement;
83
84
85
+=
86
87
88
ContextMenuservice.GetContextMenu(_splitElement);
89
90
{
91
92
_contextMenu.opened
93
_contextMenu.Closed
94
}
95
96
97
98
99
Called when the Button is clicked.
100
101
OnClick()
102
103
(IsMouSEOverSplitElement)
104
105
OpenButtonMenu();
106
107
else
108
109
.onClick();
110
111
112
113
114
Called when a key is pressed.
115
116
OnKeyDown(KeyEventArgs E)
117
118
==
E)
119
120
throw
argumentNullException(
e
);
121
122
123
((Key.Down
e.Key)
||
(Key.Up
e.Key))
124
125
WPF requires this to happen via BeginInvoke
126
Dispatcher.beginInvoke((Action)(()
=>
OpenButtonMenu()));
127
128
129
130
.onKeyDown(E);
131
132
133
134
135
Opens the button menu.
136
137
OpenButtonMenu()
138
139
((
0
_buttonMenuItemssource.Count)
&&
_contextMenu))
140
141
_contextMenu.HorizontalOffset
142
_contextMenu.VerticalOffset
143
_contextMenu.IsOpen
true
144
145
146
147
148
Called when the mouse goes over the split element.
149
</sumMary>
150
<param NAME="sender">
Event source.
</param>
151
<param NAME="e">
Event arguments.
</param>
152
SplitElement_MouseEnter(
sender, MouseEventArgs E)
153
154
IsMouSEOverSplitElement
155
156
157
158
Called when the mouse goes off the split element.
159
160
161
162
SplitElement_MouSELEave(
163
164
false
165
166
167
168
Called when the ContextMenu is opened.
169
170
171
172
ContextMenu_Opened(
:rgb(0, routedEventArgs E)
173
174
Offset the ContextMenu correctly
175
176
_contextMenuInitialOffset
_contextMenu.TransformToVisual(
).Transform(
Point());
177
178
UPDATEContextMenuOffsets();
179
180
Hook Layoutupdated to handle application resize and zoom changes
181
Layoutupdated
EventHandler(SplitButton_Layoutupdated);
182
183
184
185
Called when the ContextMenu is closed.
186
187
188
189
ContextMenu_Closed(
190
191
No longer need to handle Layoutupdated
192
193
194
restore focus to the Button
195
Focus();
196
197
198
199
Called when the ContextMenu is open and layout is UPDATEd.
200
201
202
203
SplitButton_Layoutupdated(
:rgb(0, EventArgs E)
204
205
206
207
208
209
UPDATEs the ContextMenu's Horizontal/VerticalOffset properties to keep it under the SplitButton.
210
211
UPDATEContextMenuOffsets()
212
213
Calculate desired offset to put the ContextMenu below and left-aligned to the Button
214
215
Point currentOffset
_contextMenuInitialOffset;
216
Point desiredOffset
TransformToVisual(Application.Current.RootVisual).Transform(
Point(
, ActualHeight));
217
218
_contextMenu.HorizontalOffset
desiredOffset.X
-
currentOffset.X;
219
_contextMenu.VerticalOffset
desiredOffset.Y
currentOffset.Y;
220
Adjust for rTL
221
(FlowDirection.RightToLeft
FlowDirection)
222
223
_contextMenu.updateLayout();
224
_contextMenu.ActualWidth;
225
226
227
228
229
}
230
}
Silverlight工具栏 Toolbar源码
Grid
Height
="33"
HorizontalAlignment
="Stretch"
Margin
="0,5,0"
NAME
="grid1"
VerticalAlignment
="Top"
="Auto"
Border
BorderThickness
="0"
BACkground
="Transparent"
CornerRadius
="3"
BorderBrush
="Gray"
Grid
Grid.columnDeFinitions
columnDeFinition
Width
="*"
="Auto"
sdk:DataPager
Grid.column
="1"
="24"
="Left"
="dataPager1"
PageSize
="10"
="169"
BorderThickness
AutoEllipsis
="True"
AllowDrop
="false"
Displaymode
="FirstLastPrevIoUsNext"
Is@R_902_10586@lItemCountFixed
StackPanel
="stackPanel1"
="Center"
Orientation
="Horizontal"
Button
="30"
Padding
="5"
cursor
="Hand"
="tsbAddNew"
="Transparent"
Button.Content
Orientation
Opacity
="0.7"
="-2,-2,0)"> source
="/SilverlightApplication2;component/images/plus.png"
="16"
TextBlock
Text
="Add New Person"
="5,0)">StackPanel
Button
Padding
="tsbTest3"
="10,255)">="/SilverlightApplication2;component/images/zoom.png"
="Advanced Search"
="tsbTest"
="/SilverlightApplication2;component/images/refresh2.png"
="refresh"
Border
>
菜单、弹出式菜单和鼠标经过按钮出现的下拉菜单
这个开源(LPGL协议)的不错哦,用法是这样的:
Name
="Button1"
="50"
/>
@H_324_74
@my:PopupMenu
RightClickElements
LeftClickElements
AccessShortcut
="Ctrl+Alt+M"
>
ListBox
>
@H_324_74
@my:PopupMenuItem
="Menu1"
/>
@H_324_74
@my:PopupMenuSeparator
="SubMenuHeader1"
ImageRightsource
="images/arrow.png"
>
@H_324_74
@my:PopupMenu
>
>
="SubMenu1"
/>
@H_324_74
@my:PopupMenuItem
="Menu2"
="Menu3"
/>
>
>
在CodeBehind里面:
这样鼠标经过button1就会浮出菜单。同时@L_377_50@mVVM的Command和CommandParameter,不错。
大佬总结
以上是大佬教程为你收集整理的Silverlight入门系列]右键菜单ContextMenu工具栏Toolbar和SplitButton全部内容,希望文章能够帮你解决Silverlight入门系列]右键菜单ContextMenu工具栏Toolbar和SplitButton所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。