如何创建 WPF 圆角容器?
我们正在创建一个 XBAP 应用程序,需要在单个页面的不同位置具有圆角,并且希望有一个 WPF 圆角容器来在其中放置一堆其他元素。 有人对我们如何最好地实现这一目标有一些建议或示例代码吗? 是使用样式还是创建自定义控件?
We are creating an XBAP application that we need to have rounded corners in various locations in a single page and we would like to have a WPF Rounded Corner container to place a bunch of other elements within. Does anyone have some suggestions or sample code on how we can best accomplish this? Either with styles on a or with creating a custom control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您尝试将按钮放在圆角矩形边框中,则应该查看 msdn 的示例。 我通过谷歌搜索问题的图像(而不是文本)发现了这一点。 值得庆幸的是,它们庞大的外部矩形很容易移除。
请注意,您必须重新定义按钮的行为(因为您更改了 ControlTemplate)。 也就是说,您需要使用 ControlTemplate.Triggers 标记中的 Trigger 标记 (Property="IsPressed" Value="true") 定义单击时按钮的行为。 希望这可以节省我失去的时间:)
If you're trying to put a button in a rounded-rectangle border, you should check out msdn's example. I found this by googling for images of the problem (instead of text). Their bulky outer rectangle is (thankfully) easy to remove.
Note that you will have to redefine the button's behavior (since you've changed the ControlTemplate). That is, you will need to define the button's behavior when clicked using a Trigger tag (Property="IsPressed" Value="true") in the ControlTemplate.Triggers tag. Hope this saves someone else the time I lost :)
kobusb 边界控制解决方案的基于 VB.Net 代码的实现。 我用它来填充按钮控件的列表框。 Button 控件是从 MEF 扩展创建的。 每个扩展都使用 MEF 的 ExportMetaData 属性作为扩展的描述。 扩展是 VisiFire 图表对象。 用户按下从按钮列表中选择的按钮来执行所需的图表。
VB.Net code based implementation of kobusb's Border control solution. I used it to populate a ListBox of Button controls. The Button controls are created from MEF extensions. Each extension uses MEF's ExportMetaData attribute for a Description of the extension. The extensions are VisiFire charting objects. The user pushes a button, selected from the list of buttons, to execute the desired chart.
您不需要自定义控件,只需将容器放入边框元素中即可:
您可以将
替换为任何布局容器...You don't need a custom control, just put your container in a border element:
You can replace the
<Grid/>
with any of the layout containers...我知道这不是最初问题的答案......但您经常想要剪辑刚刚创建的圆角边框的内部内容。
Chris Cavanagh 提出了一个绝妙方法< /a> 就是为了做到这一点。
我尝试了几种不同的方法……我认为这个很有效。
下面是 xaml:
I know that this isn't an answer to the initial question ... but you often want to clip the inner content of that rounded corner border you just created.
Chris Cavanagh has come up with an excellent way to do just this.
I have tried a couple different approaches to this ... and I think this one rocks.
Here is the xaml below:
我只需要自己做这件事,所以我想我会在这里发布另一个答案。
这是创建圆角边框并剪辑其内部内容的另一种方法。 这是使用 Clip 属性的直接方法。 如果您想避免使用 VisualBrush,那就太好了。
xaml:
转换器的代码:
I just had to do this myself, so I thought I would post another answer here.
Here is another way to create a rounded corner border and clip its inner content. This is the straightforward way by using the Clip property. It's nice if you want to avoid a VisualBrush.
The xaml:
The code for the converter: