Silverlight 中的圆角矩形
我正在尝试在 Silverlight 中创建一个矩形,其角是圆角的。但是,我没有明确指定矩形的宽度和高度,这意味着它会适应包含它的网格的大小(网格的大小取决于屏幕分辨率等,并且事先未知) )。
我希望 RadiusX 和 RadiusY 属性分别是矩形宽度和高度的百分比。这样做最干净的方法是什么?是否有一种仅 XAML 的方法(无需诉诸代码隐藏)?
I'm trying to create a rectangle in Silverlight where the corners are rounded. However, I do not explicitly specify the width and the height of the rectangle, which means it adapts to the size of the Grid which contains it (the size of the grid depends on the screen resolution amongst other things, and is not known before hand).
I'd like the RadiusX and RadiusY properties to be percentages of the rectangle's width and height respectively. What would be the cleanest way of doing this? Is there a XAML-only way of doing it (without resorting to code-behind)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面两个文件可供下载,我用于测试此 http://dl.dropbox.com/u /8679840/SilverlightApplication1.zip
重用的最佳方法是创建一个类型转换器,例如
以及后面的代码
Two files below to download I used for testing this http://dl.dropbox.com/u/8679840/SilverlightApplication1.zip
Best way for reuse is to create a Type converter like
and the code behind
虽然贾斯汀·金的答案在事先知道宽度和高度的情况下有效,但如果未设置它们,则它不起作用,并且父控件会动态地布置矩形。不幸的是,在 Silverlight 中,您无法在 ActualWidth 和 ActualHeight 上使用转换器绑定,因为它们是计算属性。这意味着当 ActualWidth 和 ActualHeight 更改时,不会在内部引发属性更改事件,因此绑定不会将更改传播到源。
本质上,此时唯一的选择是订阅 LayoutUpdated 事件并在代码隐藏中计算和设置 RadiusX 和 RadiusY 属性。
While Justin King's answer works if the Width and Height are known before hand, it doesn't work if they're not set, and the parent control dynamically lays the rectangle out. Unfortunately, in Silverlight, you cannot use Binding with Converters on ActualWidth and ActualHeight, as they are calculated properties. What this means is that when ActualWidth and ActualHeight change, a property changed event is not raised internally, so the binding wouldn't propagate the changes to the source.
Essentially, at this point, the only option is to subscribe to the LayoutUpdated event and calculate and set the RadiusX and RadiusY properties in code-behind.