Silverlight 中的圆角矩形

发布于 2024-12-06 17:18:05 字数 190 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失而复得 2024-12-13 17:18:05

下面两个文件可供下载,我用于测试此 http://dl.dropbox.com/u /8679840/SilverlightApplication1.zip

重用的最佳方法是创建一个类型转换器,例如

<Grid x:Name="LayoutRoot" Background="White">
    <Rectangle x:Name="rectangle" 
    Width="200" Height="200"
    RadiusX="{Binding Width, ElementName=rectangle, Converter={StaticResource myConverter}, ConverterParameter=.1}" 
    RadiusY="{Binding Height, ElementName=rectangle, Converter={StaticResource myConverter}, ConverterParameter=.1}"
    />
</Grid>

以及后面的代码

namespace SilverlightApplication1
{
      public class PercentConverter : IValueConverter 
    { 
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            return System.Convert.ToDouble(value) * System.Convert.ToDouble(parameter); 
        } 

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
    }
}

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

<Grid x:Name="LayoutRoot" Background="White">
    <Rectangle x:Name="rectangle" 
    Width="200" Height="200"
    RadiusX="{Binding Width, ElementName=rectangle, Converter={StaticResource myConverter}, ConverterParameter=.1}" 
    RadiusY="{Binding Height, ElementName=rectangle, Converter={StaticResource myConverter}, ConverterParameter=.1}"
    />
</Grid>

and the code behind

namespace SilverlightApplication1
{
      public class PercentConverter : IValueConverter 
    { 
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            return System.Convert.ToDouble(value) * System.Convert.ToDouble(parameter); 
        } 

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
    }
}
诗酒趁年少 2024-12-13 17:18:05

虽然贾斯汀·金的答案在事先知道宽度和高度的情况下有效,但如果未设置它们,则它不起作用,并且父控件会动态地布置矩形。不幸的是,在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文