如何创建不规则形状的UserControl?
在我的 Silverlight 4 应用程序中,我需要创建一个具有不规则形状的用户控件。 UC 的“主显示”是一个标准矩形,但我需要在主显示矩形之外有选项卡(简单的文本块,用户可以单击)。
Silverlight 4 可以实现这一点吗?如果是这样,怎么办?
提前致谢。
In my Silverlight 4 application I need to create a user control with an irregular shape. The "main display" of the UC is a standard rectangle but I need to have tabs (simple text blocks, where the user can click) that are outside of the main display rectangle.
Is this possible with Silverlight 4? If so, how?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过多种方式将控件的元素放置在其正常布局之外。您可以使用
Canvas
,但如果大部分控件是标准网格矩形,那么您可以使用网格。诀窍是使用负边距。请注意,如果用户控件被用作视觉根,那么这将不起作用,因为 Silverlight 插件不会呈现超出其客户端矩形的范围。
You can position elements of a control outside its normal layout in a number of ways. You could use
Canvas
but if most of the control is standard Grid rectangle then you can use a Grid. The trick is to use negative Margins.Note that if the Usercontrol is being used as the Visual root then this won't work because the Silverlight plugin will not render beyound its client rectangle.
事实上,您可以在选项卡后面设置透明背景,这样可以让点击通过,有效地表现得好像形状不同一样。 UserControl 仍将具有包含选项卡的矩形形状,除非您将其包装到 Popup 中并以一定的偏移量从 UC 中浮动。
It is, you can have transparent background behind the tabs which can let clicks through, effectively behaving as if the shape was different. The UserControl will still have a rectangular shape including the tabs, unless you wrap then into a Popup and float out of the UC with some offset.
从技术上讲,如果您使用 Canvas 作为
LayoutRoot
而不是Grid
,则可以在UserControl
矩形之外包含元素。画布中的元素不会根据画布大小进行裁剪。但是,我不建议这样做,因为您将无法使用Margin
来调整其中的控件大小和对齐。最好将所有子控件都放在Grid
LayoutRoot
中。这给我们带来了不规则性的问题。如果您想“看透”控件的某些部分并能够单击它们(即单击其下方的对象),您所需要做的就是保留
UserControl
和LayoutRoot
的Background
为null
或者根本不设置它。只要缺乏任何背景,点击就会通过。请注意,如果将背景设置为透明
,它将使控件对于鼠标输入表现为矩形(就好像它填充有纯色)。另一件事是,如果您想在应用程序的透明部分下看到 HTML 控件。然后,您将不得不使用无窗口模式,但这是另一种蠕虫。
Technically, you can have elements outside the
UserControl
's rectangle if you use a Canvas for yourLayoutRoot
instead of aGrid
. Elements in a Canvas aren't clipped to the canvas size. I wouldn't recommend this, however, because you won't be able to useMargin
to size and align your controls inside it. It would be better to have all child controls inside aGrid
LayoutRoot
.Which brings us to the question of irregularity. If you want to 'see through' parts of the control and be able to click through them (i.e. click objects underneath it), all you need to do is keep the
UserControl
's and theLayoutRoot
'sBackground
tonull
or just not set it at all. Wherever there is a lack of any background, clicks will go through. Note that if you set the background toTransparent
it will make the control behave as a rectangle (as if it's filled with solid color) with respect to mouse input.Another thing is if you want to see HTML controls under the see-through parts of your app. Then, you'll have to use windowless mode, but that's another can of worms.