无法从静态资源设置 CornerRadius 值
我定义了一个静态资源:
<UserControl x:Class="MyProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Width="255"
Height="300">
<UserControl.Resources>
<sys:Double x:Key="CornerRadiusValue">5</sys:Double>
</UserControl.Resources>
...
稍后在 XAML 文件中,我尝试在设置边框的左上角半径时使用该值:
<Border
Width="40"
Height="30"
BorderThickness="1,1,0,0"
BorderBrush="Red">
<Border.CornerRadius>
<CornerRadius TopLeft="{StaticResource CornerRadiusValue}" />
</Border.CornerRadius>
</Border>
在设计时,一切正常并更改 CornerRadiusValue
的值静态资源更改边框上的角半径。但是,当我想运行它时,我收到一个 XamlParseException
异常,并显示以下消息:
无法设置只读属性“System.Windows.CornerRadius.TopLeft”。
我做错了什么?我该如何让它发挥作用?谢谢。
I have defined a static resource:
<UserControl x:Class="MyProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Width="255"
Height="300">
<UserControl.Resources>
<sys:Double x:Key="CornerRadiusValue">5</sys:Double>
</UserControl.Resources>
...
Later on in XAML file, I am trying to use that value when setting top left corner radius for a border:
<Border
Width="40"
Height="30"
BorderThickness="1,1,0,0"
BorderBrush="Red">
<Border.CornerRadius>
<CornerRadius TopLeft="{StaticResource CornerRadiusValue}" />
</Border.CornerRadius>
</Border>
In design time, everything works fine and changing the value for CornerRadiusValue
static resource changes the corner radius on a border. However, when I want to run this, I get an XamlParseException
exception with the message:
Cannot set read-only property `System.Windows.CornerRadius.TopLeft'.
What am I doing wrong? How do I make it work? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MSDN :
您可以尝试绑定整个 CornerRadius 属性并使用转换器获取所有资源,并使用构造函数从中创建一个 CornerRadius 实例。
例如,仅使用一个值:
您可能可以通过搜索树上的资源而不是直接针对定义资源的对象来使其更加通用。
(这是仅 Silverlight 的问题,在 WPF 中您的代码工作得很好,如果您有 Silverlight 问题,请避免使用 WPF 标记,除非问题实际上存在于 WPF 中)
MSDN:
You could try to bind the whole
CornerRadius
property and use a converter to get all the resources and create aCornerRadius
instance from them using the constructor.e.g. using only one value:
You could propbably make this more generic by searching for the resources going up the tree and not directly targeting the object on which the resources are defined.
(This is a Silverlight-only problem, in WPF your code works just fine, if you have a Silverlight question please avoid the WPF tag unless the problem actually exists in WPF)
我相信
CornerRadius
属性不是DependencyProperties
,因此无法通过绑定设置它们。我能想到的两种选择是使用 MultiConverter 接受 Border 对象的参数和所需的 CornerRadius ,或者创建一个CornerRadius 的自定义 DependencyProperty。这两种方法都需要您在代码隐藏中设置值。
I believe
CornerRadius
properties are notDependencyProperties
, so they cannot be set through a binding.The two alternatives I can think of is to use a
MultiConverter
which accepts the parameters of theBorder
object, and desiredCornerRadius
, or to create a custom DependencyProperty for theCornerRadius
. Both methods would require you to set the value in code-behind.