无法从静态资源设置 CornerRadius 值

发布于 2025-01-02 00:26:56 字数 1179 浏览 3 评论 0原文

我定义了一个静态资源:

<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 技术交流群。

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

发布评论

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

评论(3

糖果控 2025-01-09 00:26:56
<sys:Double x:Key="ScrollbarHandleCrValue">3</sys:Double>
<CornerRadius x:Key="ScrollbarHandleCornerRadius" 
   TopLeft="{StaticResource ScrollbarHandleCrValue}" 
   TopRight="{StaticResource ScrollbarHandleCrValue}" 
   BottomRight="{StaticResource ScrollbarHandleCrValue}" 
   BottomLeft="{StaticResource ScrollbarHandleCrValue}" />

...
<Border Name="Border"
   CornerRadius="{StaticResource ScrollbarHandleCornerRadius}" 
   Background="{StaticResource ScrollbarHandleColor}"
   BorderBrush="Transparent"
   BorderThickness="1" />
...
<sys:Double x:Key="ScrollbarHandleCrValue">3</sys:Double>
<CornerRadius x:Key="ScrollbarHandleCornerRadius" 
   TopLeft="{StaticResource ScrollbarHandleCrValue}" 
   TopRight="{StaticResource ScrollbarHandleCrValue}" 
   BottomRight="{StaticResource ScrollbarHandleCrValue}" 
   BottomLeft="{StaticResource ScrollbarHandleCrValue}" />

...
<Border Name="Border"
   CornerRadius="{StaticResource ScrollbarHandleCornerRadius}" 
   Background="{StaticResource ScrollbarHandleColor}"
   BorderBrush="Transparent"
   BorderThickness="1" />
...
唔猫 2025-01-09 00:26:56

MSDN :

您可以在 XAML 中设置此值,但只能作为采用 CornerRadius,或作为 CornerRadius 对象元素。请参阅 的 XAML 部分和备注部分圆角半径

您可以尝试绑定整个 CornerRadius 属性并使用转换器获取所有资源,并使用构造函数从中创建一个 CornerRadius 实例。

例如,仅使用一个值:

<Border Name="bd" BorderBrush="Red" BorderThickness="1">
    <Border.Resources>
        <sys:Double x:Key="CR_TopLeft">5</sys:Double>
    </Border.Resources>
    <Border.CornerRadius>
        <Binding ElementName="bd">
            <Binding.Converter>
                <vc:CornerRadiusConverter />
            </Binding.Converter>
        </Binding>
    </Border.CornerRadius>
    <Button>!</Button>
</Border>
public class CornerRadiusConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var resourceSource = (FrameworkElement)value;
        var topLeft = (double)resourceSource.Resources["CR_TopLeft"];
        return new CornerRadius(topLeft, 0, 0, 0);
    }

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

您可能可以通过搜索树上的资源而不是直接针对定义资源的对象来使其更加通用。

(这是仅 Silverlight 的问题,在 WPF 中您的代码工作得很好,如果您有 Silverlight 问题,请避免使用 WPF 标记,除非问题实际上存在于 WPF 中)

MSDN:

You can set this value in XAML, but only as part of the attribute syntax for properties that take a CornerRadius, or as initialization text of a CornerRadius object element. See XAML sections and Remarks sections of CornerRadius.

You could try to bind the whole CornerRadius property and use a converter to get all the resources and create a CornerRadius instance from them using the constructor.

e.g. using only one value:

<Border Name="bd" BorderBrush="Red" BorderThickness="1">
    <Border.Resources>
        <sys:Double x:Key="CR_TopLeft">5</sys:Double>
    </Border.Resources>
    <Border.CornerRadius>
        <Binding ElementName="bd">
            <Binding.Converter>
                <vc:CornerRadiusConverter />
            </Binding.Converter>
        </Binding>
    </Border.CornerRadius>
    <Button>!</Button>
</Border>
public class CornerRadiusConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var resourceSource = (FrameworkElement)value;
        var topLeft = (double)resourceSource.Resources["CR_TopLeft"];
        return new CornerRadius(topLeft, 0, 0, 0);
    }

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

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)

倾城花音 2025-01-09 00:26:56

我相信 CornerRadius 属性不是 DependencyProperties,因此无法通过绑定设置它们。

我能想到的两种选择是使用 MultiConverter 接受 Border 对象的参数和所需的 CornerRadius ,或者创建一个CornerRadius 的自定义 DependencyProperty。这两种方法都需要您在代码隐藏中设置值。

I believe CornerRadius properties are not DependencyProperties, 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 the Border object, and desired CornerRadius, or to create a custom DependencyProperty for the CornerRadius. Both methods would require you to set the value in code-behind.

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