none
在样式 style 中定义依赖属性值的错误 RRS feed

  • 问题

  • xaml 有如下border 

    <UserControl x:Class="Ai2.Wpf.Controls.Buttons.Ai2Button"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:Ai2.Wpf.Controls.Buttons"
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
        <Border x:Name="BorderContainer" Background="White" 
                CornerRadius="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=CornerRadius, Mode=OneWay}"
                BorderThickness="{Binding ElementName=this, Path=ButtonThickness}">
    
        </Border>
    </UserControl>

    对应的cs文件里定义了依赖属性

            public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register(nameof(CornerRadius), typeof(CornerRadius), typeof(Ai2Button), 
                new PropertyMetadata(4));
    
            public CornerRadius CornerRadius
            {
                get => (CornerRadius)GetValue(CornerRadiusProperty);
                set => SetValue(CornerRadiusProperty, value);
            }

    然而,我在定义 style 时,有提示错误

        <Style x:Key="Ai2ButtonDefaultStyle" TargetType="{x:Type btn:Ai2Button}">
            <Setter Property="CornerRadius" Value="4"></Setter>
            <Setter Property="ButtonThickness" Value="1"></Setter>
        </Style>

    错误如下:

    Error	XDG0041	The property "CornerRadius" is not a DependencyProperty. To be used in markup, non-attached properties must be exposed on the target type with an accessible instance property "CornerRadius". For attached properties, the declaring type must provide static "GetCornerRadius"  and "SetCornerRadius" methods.
    不知道为什么, 如何解决?

    2021年7月4日 2:07

答案


  • CornerRadius是一个依赖属性。因为CornerRadiusProperty注册的类型为CornerRadius所以我将代码:     

    public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register(nameof(CornerRadius), typeof(CornerRadius), typeof(Ai2Button), 
      new PropertyMetadata(4));

    修改为

    public static readonly DependencyPropertyCornerRadiusProperty =DependencyProperty.Register(nameof(CornerRadius),typeof(CornerRadius), typeof(Ai2Button),
      new PropertyMetadata(new CornerRadius(4)));

    我在MainWindow.xaml中使用UserControl的代码如下:

    <Window.Resources>
        <Style x:Key="Ai2ButtonDefaultStyle" TargetType="local:Ai2Button">
           <Setter Property="CornerRadius" Value="4"></Setter>
           <Setter Property="ButtonThickness"Value="1"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
       <Border Width="200"Height="50"  Background="Red">
           <local:Ai2Button Width="180" Height="48"Style="{StaticResource Ai2ButtonDefaultStyle}"/>
       </Border>
    </Grid>

    它可以正常运行,没有重现您的问题,是我少做了什么步骤吗?有任何问题请告诉我。

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    <audio controls="controls" style="display:none;"></audio>
    2021年7月5日 6:43